Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Advent 2011 Day 7: Crosstab with PIVOTShared Connections – New level of efficiency in SSIS 2012 »
    comments

    In this short blog post I would like to share a script I wrote yesterday as an answer to this MSDN thread to find maximum length of data in every column of a table passed as a parameter.

    Some notes for the script:

    LEN function can be used with many SQL Server types excluding text and new types such as Geography or HierarchyID. I added exclusion of these types directly in the query, there may be some other types which need to be also excluded.

    I am using one of my favorite ideas of generating script to run using available meta-data.

    1. USE AdventureWorks2008R2
    2.  
    3. --declare @TableName sysname = 'Employee', @TableSchema sysname = 'HumanResources'
    4. DECLARE @TableName sysname = 'Address', @TableSchema sysname = 'Person'
    5. DECLARE @SQL NVARCHAR(MAX)
    6.  
    7. SELECT @SQL = STUFF((SELECT
    8. '
    9. UNION ALL
    10. select ' + QUOTENAME(Table_Name,'''') + ' AS Table_Name, ' +
    11. QUOTENAME(Column_Name,'''') + ' AS ColumnName, MAX(LEN(' + QUOTENAME(Column_Name) +
    12. ')) as [Max Length], ' + QUOTENAME(C.DATA_TYPE,'''') + ' AS Data_Type, ' +
    13. CAST(COALESCE(C.CHARACTER_MAXIMUM_LENGTH, C.NUMERIC_SCALE,0) AS VARCHAR(10)) +
    14. '  AS Data_Width FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(Table_Name)
    15. FROM INFORMATION_SCHEMA.COLUMNS C
    16. WHERE TABLE_NAME = @TableName
    17. AND table_schema = @TableSchema
    18. AND DATA_TYPE NOT IN ('text','ntext','XML','HierarchyID','Geometry','Geography')
    19. ORDER BY COLUMN_NAME
    20. FOR XML PATH(''),Type).value('.','varchar(max)'),1,11,'')  
    21. --print @SQL
    22. EXECUTE (@SQL)

    Here is another version of the same script which attempts to list every column in a table but uses DATALENGTH function instead of LEN for the types where we can not use LEN function:

    1. USE AdventureWorks2008R2
    2. --declare @TableName sysname = 'Employee', @TableSchema sysname = 'HumanResources'
    3. DECLARE @TableName sysname = 'Address', @TableSchema sysname = 'Person'
    4. DECLARE @SQL NVARCHAR(MAX)
    5.  
    6. SELECT @SQL = STUFF((SELECT
    7. '
    8. UNION ALL
    9. select ' + QUOTENAME(Table_Name,'''') + ' AS Table_Name, ' +
    10. QUOTENAME(Column_Name,'''') + ' AS ColumnName, MAX(' +
    11. CASE WHEN DATA_TYPE IN ('XML','HierarchyID','Geometry','Geography','text','ntext')
    12.      THEN 'DATALENGTH(' ELSE 'LEN(' END + QUOTENAME(Column_Name) +
    13. ')) as [Max Length], ' + QUOTENAME(C.DATA_TYPE,'''') + ' AS Data_Type, ' +
    14. CAST(COALESCE(C.CHARACTER_MAXIMUM_LENGTH, C.NUMERIC_SCALE,0) AS VARCHAR(10)) +
    15. '  AS Data_Width FROM ' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(Table_Name)
    16. FROM INFORMATION_SCHEMA.COLUMNS C
    17. WHERE TABLE_NAME = @TableName
    18. AND table_schema = @TableSchema
    19. --AND DATA_TYPE NOT IN ('XML','HierarchyID','Geometry','Geography')
    20. ORDER BY COLUMN_NAME
    21. FOR XML PATH(''),Type).value('.','varchar(max)'),1,11,'')  
    22. --print @SQL
    23. EXECUTE (@SQL)

    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    Naomi Nosonovsky, Senior Software Developer, has more than 15 years of enterprise experience in analysis, design, development and implementation of high-performance client/server data management solutions. She is a Personality of the Year for 2008, 2009, 2010 and 2011 at www.universalthread.com in .NET, SQL Server & VFP categories. She is also an All Star contributor/MCC Community Contributor at forums.asp.net and MSDN T-SQL forum.
    Social SitingsFacebookLinkedIndeliciousLTD RSS Feed
    657 views
    Instapaper

    4 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis you know LEN already trims right? However datalength doesn't


    DECLARE @d CHAR(10) ='123 '
    SELECT LEN(@d), DATALENGTH(@d)

    --------
    3 10

    there is also a difference for unicode

    DECLARE @d NCHAR(10) ='123 '
    SELECT LEN(@d), DATALENGTH(@d)

    --------
    3 20


    Datalength can also be used with text and ntext columns

    See also The differences between LEN and DATALENGTH in SQL Server
    12/06/11 @ 08:48
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky You're absolutely right, we don't need LTRIM in this code (I removed it). However, LEN function still can not be applied to text and other types, I've verified.
    12/06/11 @ 09:00
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Also, the script can be adjusted to use DATALENGTH for the types where we can not use LEN.
    12/06/11 @ 09:04
    Comment from: SQLDenis [Member] Email
    SQLDenis just remember that DATALENGTH does not trim, I guess you can always convert to varchar(max)
    12/06/11 @ 09:10

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)