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?
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.
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.
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.
SELECT LEFT(field1,LOCATE(' ',field1) - 1)
Note that if the string in question contains no spaces, this will return an empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With