Find the length of the longest text in a column

If you have a requirement to find out the longest text in a varchar, nvarchar or a text column, then here's how to do so:


 


USE PUBS


SELECT MAX(DATALENGTH(title)) as TextLength FROM titles




In order to print the longest text in a descending order of the length of text, here's how to do so:


 


USE PUBS


SELECT title, DATALENGTH(title) as TextLength FROM titles


group by title


order by TextLength desc


1 comment: