SQL Server Admin
T-SQL Articles

March 06, 2009

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



Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

1 Response to "Find the length of the longest text in a column"
  1. Agustin Garzon said...
    March 6, 2010 7:25 AM

    Useful post.

    Regards!

    Agustin.

 

Copyright © 2009-2012 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions