How to query max(len) from all columns?

11.4k views Asked by At

I have temp table which contains nearly 200 columns. I need to know the max length of each column such that I can rectify the warning ''string or binary data would be truncated''. Now I can't mention all columns like

select max(len(col1)),max(len(col2))....,,, from #tableA

Now how can I get max length of all columns?

4

There are 4 answers

1
Neo Vijay On

Try this :

SELECT COLUMN_NAME,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'

Please find more information here How can I get column names from a table in SQL Server?

0
Deepshikha On

To get max length of all columns of temp table you can write a query as:

select name,max_length
from tempdb.sys.columns 
where object_id = object_id('tempdb..#Temp');
0
knkarthick24 On

Yes you can use INFORMATION_SCHEMA.COLUMNS for any DB.

Prefix your db name. In your case use tempdb.INFORMATION_SCHEMA.COLUMNS

SELECT COLUMN_NAME,
       CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION
FROM   tempdb.INFORMATION_SCHEMA.COLUMNS
WHERE table_name like  '%tableA%'
1
AudioBubble On

I had this exact same issue, and found the code below (complete copy/paste from the site) which did the trick for me. Unfortunately, I had to go through a few iterations of data loading to work out every column that was the culprit, but was able to resolve everything in the end:

/**************************************
SQL to get max length of values in every table column.

This SQL queries the specified table to get the max length 
of all the values in every column.  

To work, load all the incoming data into a "permanent" temporary table 
where every column is defined as a varchar(max).

Then run this script against that "permanent" temporary table, comparing 
the results against the original table's schema to see what column, and
then what record(s), is/are causing the issue.

Example, if the max length of a value in one of the columns is 270, 
but your original table's schema is varchar(255), obviously either 
the original table's schema will have to be altered or the data corrected. 

Code from: http://cc.davelozinski.com
**************************************/

--The table we'll be performing this query on to get the lengths of every column.
--Default is dbo schema. Change as appropriate for your table.
DECLARE @TableName VARCHAR(200) = 'Your table name to query'
       ,@SchemaName VARCHAR(200) = 'dbo'

DECLARE @MaxLengthDefault INT
       ,@Column VARCHAR(50)
       ,@MaxLength INT
       ,@MaxLengthString VARCHAR(10)
       ,@ColumnID INT
       ,@MaxColumnID INT
       ,@Command VARCHAR(2000)

CREATE TABLE #Temp (
       column_name VARCHAR(50)
       ,max_length INT
       ,max_length_default INT
)

SELECT @ColumnID = min(b.[column_id])
       ,@MaxColumnID = max(b.[column_id]) 
FROM sys.tables a
INNER JOIN sys.columns b on a.[object_id] = b.[object_id] 
WHERE a.[name] = @TableName 
       and SCHEMA_NAME(a.[schema_id]) = @SchemaName    

--SELECT @ColumnID, @MaxColumnID

WHILE(@ColumnID <= @MaxColumnID)
BEGIN
    SET @Column = null

    SELECT @Column = b.[name]
              ,@MaxLengthDefault = b.[max_length]
       FROM sys.tables a
       INNER JOIN sys.columns b on a.[object_id] = b.[object_id]
       WHERE a.[name] = @TableName
              and SCHEMA_NAME(a.[schema_id]) = @SchemaName
              and b.[column_id] = @ColumnID

    --SELECT @Column, @MaxLengthDefault

    IF ( @Column is not null )
    BEGIN
              SET @Command = 'INSERT INTO #Temp(column_name, max_length, max_length_default) 
                SELECT ''' + @Column + '''
                ,MAX(LEN(CAST([' + @Column + '] as VARCHAR(8000))))
                ,' + CAST(@MaxLengthDefault as VARCHAR(5)) + 
                ' FROM [' + @SchemaName + '].[' + @TableName + '] 
                WHERE [' + @Column + '] IS NOT NULL'
              --SELECT @Command
              EXEC(@Command)        
       END

    SET @ColumnID = @ColumnID + 1
END

SELECT * FROM #Temp

DROP TABLE #Temp