Retrieve the maximum length of a VARCHAR column in SQL Server
Asked Answered
A

11

141

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?

Anole answered 6/3, 2012 at 21:34 Comment(0)
K
245

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.

Kittle answered 6/3, 2012 at 21:35 Comment(2)
Thank you, i had a brain fart and remembered the LEN function mid question but thought it might be good to have here anyway. Thanks mate!Anole
Just a note, if you want to know size as opposed to number of characters, use DATALENGTH (since you may use NVARCHAR in some places, or someday).Russo
S
63

For MySQL, it's LENGTH, not LEN:

SELECT MAX(LENGTH(Desc)) FROM table_name
Statutory answered 4/7, 2014 at 9:24 Comment(2)
Same 'LENGTH' function for postgresql as well. (not 'LEN')Yogini
How does this answer the question?Millennial
C
22

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
Carpio answered 21/11, 2014 at 14:6 Comment(1)
Good point. Actualy the varchars 'asd' and 'asd ' are treated the same by SQL Server (except in LIKE clause). For details check: support.microsoft.com/en-us/help/316626/…Journalistic
J
10

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.

Jumbuck answered 28/1, 2015 at 20:42 Comment(0)
D
9

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.

Dislocate answered 22/6, 2016 at 13:59 Comment(0)
A
9

For SQL server (SSMS)

Option 1:

-- This returns number of characters
select MAX(LEN(ColumnName)) from table_name

Option 2:

-- This returns the number of bytes
select MAX(DATALENGTH(ColumnName)) from table_name

If you're using VARCHAR, use DATALENGTH. More details

Analyse answered 18/10, 2016 at 9:21 Comment(0)
N
5
SELECT TOP 1 column_name, LEN(column_name) AS Lenght FROM table_name ORDER BY LEN(column_name) DESC
Necrophilia answered 28/12, 2018 at 13:20 Comment(3)
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Aric
+1 for pointing out I used a SQL Server keyword DESC as a column name, which it allows for some reason LOLAnole
Correct spelling can also be critical as some reading this may not realize 'Lenght' in the answer is actually a typo for 'length' (not that if should cause an error)Rotarian
M
5

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]
Musjid answered 4/6, 2019 at 19:41 Comment(0)
T
2
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.

Terrell answered 6/3, 2012 at 21:35 Comment(0)
A
2
SELECT MAX(LEN(Desc)) as MaxLen FROM table
Anole answered 6/3, 2012 at 21:37 Comment(0)
B
-1

For IBM Db2 its LENGTH, not LEN:

SELECT MAX(LENGTH(Desc)) FROM table_name;
Boxberry answered 31/8, 2020 at 15:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.