Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL remove space before order by clause

I have a table full of "title" that I want to order by title. Problem is a lot of them have a empty space before the title. I am thinking of writting a php script to fix it all (super easy) but I was curious how could I do:

SELECT * FROM products ORDER BY title

But at the same time trimming the title so it doesnt order on the empty space. All in the same query without changing the data. God I hope I make sense.

So I am really looking for a mysql solution to this. For what its worth I use Zend_Db so a solution using that would be even better but I can manage straight MySQL.

like image 807
Iznogood Avatar asked Dec 03 '22 04:12

Iznogood


1 Answers

You can use the TRIM function:

SELECT TRIM(title) as title, field2, field3 FROM products ORDER BY TRIM(title)

That ought to do it!

like image 91
codykrieger Avatar answered Dec 18 '22 10:12

codykrieger