tags:

views:

65

answers:

4

Morning, is it possible to order sql data rows by the length of characters?

e.g. SELECT * FROM database ORDER BY data.length()

+2  A: 
SELECT * FROM table ORDER BY length(data) desc

Where data is varchar field

Michael Pakhantsov
+7  A: 

I think you want to use this: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length

SELECT * FROM table ORDER BY CHAR_LENGTH(field)

You can use just simply LENGTH(), but beware, because it counts the byte number (which won't give you the expected result with multibyte strings).

Yorirou
+1 For using `CHAR_LENGTH` instead of `LENGTH`.
Gumbo
that's it! thanks!!
TorbenL
A: 
SELECT * FROM database ORDER BY Len(data)
Alex Reitbort
A: 
SELECT * FROM YourTable ORDER BY LENGTH(Column_Name) DESC

e.g;

SELECT * FROM Customer ORDER BY LENGTH(CustomerName) DESC
Muhammad Kashif Nadeem