Here is a table structure (e.g. test):
Field Name | Data Type |
---|---|
id | BIGINT (20) |
title | varchar(25) |
Description | Text |
A query like:
SELECT * FROM TEST ORDER BY description DESC;
But I would like to order by the field size/length of the field description.
The field type will be TEXT or BLOB.
The ORDER BY command is used to sort the result set in ascending or descending order. The ORDER BY command sorts the result set in ascending order by default. To sort the records in descending order, use the DESC keyword.
MySQL LENGTH() Function The LENGTH() function returns the length of a string (in bytes).
select char_length(col. column_name) as column_name_length, count(*) as columns, count(distinct tab. table_name) as tables from information_schema. tables as tab inner join information_schema.
SQL Server LEN() Function The LEN() function returns the length of a string. Note: Trailing spaces at the end of the string is not included when calculating the length. However, leading spaces at the start of the string is included when calculating the length.
SELECT * FROM TEST ORDER BY LENGTH(description) DESC;
The LENGTH
function gives the length of string in bytes. If you want to count (multi-byte) characters, use the CHAR_LENGTH
function instead:
SELECT * FROM TEST ORDER BY CHAR_LENGTH(description) DESC;
For those using MS SQL
SELECT * FROM TEST ORDER BY LEN(field)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With