Retrieve the maximum length of a VARCHAR column in SQL Server

SqlSql ServerString LengthMaxlength

Sql Problem Overview


I want to find the longest VARCHAR in a specific column of a SQL Server table.

Here's an example:

ID = INT IDENTITY
DESC = VARCHAR(5000)

ID | Desc
---|-----
1  | a
2  | aaa
3  | aa

What's the SQL to return 3? Since the longest value is 3 characters?

Sql Solutions


Solution 1 - Sql

Use the built-in functions for length and max on the description column:

SELECT MAX(LEN(DESC)) FROM table_name;

Note that if your table is very large, there can be performance issues.

Solution 2 - Sql

For MySQL, it's LENGTH, not LEN:

SELECT MAX(LENGTH(Desc)) FROM table_name

Solution 3 - Sql

Watch out!! If there's spaces they will not be considered by the LEN method in T-SQL. Don't let this trick you and use

select max(datalength(Desc)) from table_name

Solution 4 - Sql

For Oracle, it is also LENGTH instead of LEN

SELECT MAX(LENGTH(Desc)) FROM table_name

Also, DESC is a reserved word. Although many reserved words will still work for column names in many circumstances it is bad practice to do so, and can cause issues in some circumstances. They are reserved for a reason.

If the word Desc was just being used as an example, it should be noted that not everyone will realize that, but many will realize that it is a reserved word for Descending. Personally, I started off by using this, and then trying to figure out where the column name went because all I had were reserved words. It didn't take long to figure it out, but keep that in mind when deciding on what to substitute for your actual column name.

Solution 5 - Sql

Gives the Max Count of record in table

select max(len(Description))from Table_Name

Gives Record Having Greater Count

select Description from Table_Name group by Description having max(len(Description)) >27

Hope helps someone.

Solution 6 - Sql

For sql server (SSMS)

select MAX(LEN(ColumnName)) from table_name

This will returns number of characters.

 select MAX(DATALENGTH(ColumnName)) from table_name

This will returns number of bytes used/required.

IF some one use varchar then use DATALENGTH.More details

Solution 7 - Sql

SELECT TOP 1 column_name, LEN(column_name) AS Lenght FROM table_name ORDER BY LEN(column_name) DESC

Solution 8 - Sql

SELECT MAX(LEN(Desc)) as MaxLen FROM table

Solution 9 - Sql

Many times you want to identify the row that has that column with the longest length, especially if you are troubleshooting to find out why the length of a column on a row in a table is so much longer than any other row, for instance. This query will give you the option to list an identifier on the row in order to identify which row it is.

select ID, [description], len([description]) as descriptionlength
FROM [database1].[dbo].[table1]
where len([description]) = 
 (select max(len([description]))
  FROM [database1].[dbo].[table1]

Solution 10 - Sql

select * from table name from where length( column name) =(select MAX(Length(column  name) from table name);  

I am using subquery its 100 % work try this.

Solution 11 - Sql

For IBM Db2 its LENGTH, not LEN:

SELECT MAX(LENGTH(Desc)) FROM table_name;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMilo LaMarView Question on Stackoverflow
Solution 1 - SqlaweisView Answer on Stackoverflow
Solution 2 - SqlAbhishek GoelView Answer on Stackoverflow
Solution 3 - SqlbevadaView Answer on Stackoverflow
Solution 4 - SqlcodeguruinboiseView Answer on Stackoverflow
Solution 5 - SqlShaiju TView Answer on Stackoverflow
Solution 6 - SqlMSTdevView Answer on Stackoverflow
Solution 7 - SqlAlexandru-Codrin PanaiteView Answer on Stackoverflow
Solution 8 - SqlMilo LaMarView Answer on Stackoverflow
Solution 9 - SqldiyguyView Answer on Stackoverflow
Solution 10 - Sqlankush sononeView Answer on Stackoverflow
Solution 11 - SqlShashankView Answer on Stackoverflow