Bharanikumar
Bharanikumar

Reputation: 25733

MYSQL How to use trim in select query

my table have set of records around 50,

in the table i have column called USERNAME, but some of username leading and trailing have the white space ,

so am not getting exact order result because of white space,

So tell me how to use the trim in SELECT query ,

Thanks

Upvotes: 24

Views: 64172

Answers (2)

slon
slon

Reputation: 1030

You can use REPLACE in your query to remove all whitespaces from strings, e.g.:

SELECT REPLACE(A.Postal, ' ', '') AS Postal FROM ADDRESS

Be aware that this will remove all whitespaces, not only the leading and tailing ones.

Upvotes: 3

OMG Ponies
OMG Ponies

Reputation: 332521

You can use TRIM in the ORDER BY:

ORDER BY TRIM(username)

...but this will only trim excess space on the left and right side of the text, not in between.

Using TRIM in the SELECT is as easy as:

SELECT TRIM(username) AS username
  FROM your_table

Upvotes: 35

Related Questions