Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - How to select data by string length

SELECT * FROM table ORDER BY string_length(column); 

Is there a MySQL function to do this (of course instead of string_length)?

like image 515
Gal Avatar asked Dec 09 '09 00:12

Gal


People also ask

How do I find the length of a string in MySQL?

MySQL LENGTH() Function The LENGTH() function returns the length of a string (in bytes).

How do I query the length of a string in SQL?

Well, you can use the LEN() function to find the length of a String value in SQL Server, for example, LEN(emp_name) will give you the length of values stored in the column emp_name.

Is there LEN function in MySQL?

What's the LEN() Equivalent in MySQL? In SQL Server, you can use the LEN() function to return the number of characters in a string. This is a Transact-SQL function that can also be used in Azure databases. In MySQL, you need to use the CHAR_LENGTH() function.


1 Answers

You are looking for CHAR_LENGTH() to get the number of characters in a string.

For multi-byte charsets LENGTH() will give you the number of bytes the string occupies, while CHAR_LENGTH() will return the number of characters.

like image 126
hsz Avatar answered Sep 21 '22 10:09

hsz