Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select and sort records with biggest column size by characters?

Table Customers

ID    NAME LETTER
001   TOM  Lorem Ipsum
002   JEK  Lorem Ipsum
003   MAX  texttexttexttext...(30k letters)
004   JIZ  NULL
005   ZAK  texttexttexttext...(50k letters)

The ‘Letter’ data type in DB is ‘text’, I have some records that contain 50k+ letters even my microsoft sql client wont load the full size of it :/

Anyways I need to select IDS that have the biggest lenght by symbols at letter column

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried next one:
SELECT TOP 100 *
FROM Customers
ORDER BY CHAR_LENGTH(Letter);

but looks like my db/sql client dsnt have that function :/
also I tried len(Letter) but argument data type is invalid for len function(

>Solution :

If you want the actual number of characters in the text field, cast it to a varchar, that should work for most scenarios:

select top(100) *
from Customers
order by len(cast(letter as varchar(max))) desc

If you want bytes, text can be used with datalength

select top(100) *
from Customers
order by datalength(letter) desc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading