SQL Query: order by length of characters?

后端 未结 5 715
攒了一身酷
攒了一身酷 2021-02-02 04:55

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

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

相关标签:
5条回答
  • 2021-02-02 05:35
    SELECT * FROM table ORDER BY length(data) desc
    

    Where data is varchar field

    0 讨论(0)
  • 2021-02-02 05:42
    SELECT * FROM YourTable ORDER BY LENGTH(Column_Name) DESC
    

    e.g;

    SELECT * FROM Customer ORDER BY LENGTH(CustomerName) DESC
    
    0 讨论(0)
  • 2021-02-02 05:47

    For anyone doing with Sqlite

    SELECT * FROM table ORDER BY LENGTH(field) DESC
    
    0 讨论(0)
  • 2021-02-02 05:57

    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).

    0 讨论(0)
  • 2021-02-02 05:57
    SELECT * FROM database ORDER BY Len(data)
    
    0 讨论(0)
提交回复
热议问题