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)
I
for mysql its length not len
SELECT MAX(LENGTH(Desc)) FROM table_name
For IBM Db2 its LENGTH, not LEN:
SELECT MAX(LENGTH(Desc)) FROM table_name;
SELECT MAX(LEN(Desc)) as MaxLen FROM table
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
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.
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]