Retrieve the maximum length of a VARCHAR column in SQL Server

前端 未结 10 1756
时光说笑
时光说笑 2021-01-30 10:18

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         


        
10条回答
  •  没有蜡笔的小新
    2021-01-30 10:37

    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]
    

提交回复
热议问题