Retrieve the maximum length of a VARCHAR column in SQL Server

前端 未结 10 1651
时光说笑
时光说笑 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:25

    for mysql its length not len

    SELECT MAX(LENGTH(Desc)) FROM table_name
    
    0 讨论(0)
  • 2021-01-30 10:28

    For IBM Db2 its LENGTH, not LEN:

    SELECT MAX(LENGTH(Desc)) FROM table_name;
    
    0 讨论(0)
  • 2021-01-30 10:29
    SELECT MAX(LEN(Desc)) as MaxLen FROM table
    
    0 讨论(0)
  • 2021-01-30 10:29

    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

    0 讨论(0)
  • 2021-01-30 10:35

    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.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
提交回复
热议问题