Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all characters before space in MySQL

Tags:

sql

mysql

I would like to get all the characters in a field before a space

For example, if field1 is "chara ters"

I want it to return "chara"

What would this select statement look like?

like image 404
Alex Gordon Avatar asked Aug 12 '10 19:08

Alex Gordon


People also ask

How to get string after specific character in MySQL?

MySQL SUBSTR() function MySQL SUBSTR() returns the specified number of characters from a particular position of a given string. SUBSTR() is a synonym for SUBSTRING(). A string from which a substring is to be returned. An integer indicating a string position within the string str.

How do I extract text before a character in SQL?

Using the charindex function allows you to search for the @ sign and find its starting position and then the substring is used to extract the characters before the @ sign.

How do I separate comma separated values in MySQL?

You could use a prepared statement inside the stored procedure to achieve this. You can create the whole select query as a string inside a variable and then concatenate in the comma delimited string into its IN clause. Then you can make a prepared statement from the query string variable and execute it.


1 Answers

SELECT LEFT(field1,LOCATE(' ',field1) - 1)

Note that if the string in question contains no spaces, this will return an empty string.

like image 149
Mchl Avatar answered Oct 04 '22 23:10

Mchl