steve Kim steve Kim - 1 year ago 103
SQL Question

mysql remove all whitespace at the beginning of string

So, I realized that for my db column values, I have a whitespace in front of all strings.

(Couldn't figure out why

WHERE name = "Sean"
wasn't working and by accident, I noticed that there was a whitespace at the front of the value:
WHERE name = " Sean"

There are some values which a space in between words are needed.

UPDATE (realized that my question was not clear enough).

I am trying to update the db so that the whitespace at the beginning of each string is removed.

Thank you.

Answer Source

Try: WHERE ltrim(name) = "Sean"

Link to documentation:

EDIT: Re-read and saw your edit, and noticed you wanted to update. In which case you'd want to run this for each column which has leading whitespace. Here is an example using a dummy table name, and your name column:

UPDATE table
SET name = ltrim(name)

That will update every row and remove any leading whitespace.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download