Search code examples
mysqlstringstrip

Get rid of empty space in output while parsing the string MySQL


I have a column with values that I need to strip. The data looks like this:

name 
AFBHY | Red
GHYYU | Blue
GHYYL | Black

I need to grab just the first part of the string, the desired output is AFBHY, GHYYU and so on. My code looks like this:

SUBSTRING_INDEX(`Name`, '|', 1) as `New Name`

It returns AFBHY with the white space in the end, how can I modify the query so it returns the value without white space?


Solution

  • If you just want to trim the whitespace from the returned string, why not use TRIM:

    TRIM(SUBSTRING_INDEX(`Name`, '|', 1)) as `New Name`
    

    TRIM does exactly what it intimates with it's name, trims characters (spaces by default) from the beginning and end of a string.