SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to use the DATALENGTH function?

The following Microsoft SQL Server T-SQL code samples demonstrate the usage for the DATALENGTH function and the difference between LEN() and DATALENGTH():

 

-- The LEN function counts characters(ASCII/UNICODE) without trailing spaces (RTRIM)

-- The DATALENGTH function counts bytes allocated including trailing spaces

DECLARE @string char(5) = 'ABC'

SELECT [LEN]=LEN(@string), [DATALENGTH]=DATALENGTH(@string)

/*    LEN DATALENGTH

      3   5    */

------------

DECLARE @nstring nchar(5) = N'ABC'

SELECT [LEN]=LEN(@nstring), [DATALENGTH]=DATALENGTH(@nstring)

/*    LEN   DATALENGTH

      3     10    */

------------

DECLARE @string varchar(5) = 'ABC '

SELECT [LEN]=LEN(@string), [DATALENGTH]=DATALENGTH(@string)

/*    LEN   DATALENGTH

      3     4  */

------------

DECLARE @nstring nvarchar(5) = N'ABC '

SELECT [LEN]=LEN(@nstring), [DATALENGTH]=DATALENGTH(@nstring)

/*    LEN   DATALENGTH

      3     8  */

------------

 

------------

-- LEN/DATALENGTH Application to non-string data types

------------

DECLARE @money money = 123

SELECT [LEN]=LEN(@money), [DATALENGTH]=DATALENGTH(@money)

/*    LEN   DATALENGTH

      6     8  */

------------

DECLARE @varbinary varbinary(max) = 0xFFEEDDCCBBAA2020

SELECT [LEN]=LEN(@varbinary), [DATALENGTH]=DATALENGTH(@varbinary)

/*    LEN   DATALENGTH

      6     8  */

------------

DECLARE @binary binary(12) = 0xFFEEDDCCBBAA2020

SELECT [LEN]=LEN(@binary), [DATALENGTH]=DATALENGTH(@binary)

/*    LEN   DATALENGTH

      12    12  */

------------

 

 

USE Northwind;

 

SELECT ' OrderID,'                                    AS [Column],

       (SELECT MAX(DATALENGTH(OrderID))

        FROM   Orders)                          AS [DATALENGTH]

UNION ALL

SELECT ' ShipName,',

       (SELECT MAX(DATALENGTH(ShipName))

        FROM   Orders)

UNION ALL

SELECT ' ShipAddress,',

       (SELECT MAX(DATALENGTH(ShipAddress))

        FROM   Orders)

UNION ALL

SELECT ' ShipCity,',

       (SELECT MAX(DATALENGTH(ShipCity))

        FROM   Orders)

UNION ALL

SELECT ' ShipRegion,',

       (SELECT MAX(DATALENGTH(ShipRegion))

        FROM   Orders)

/*

Column            DATALENGTH

 OrderID,         4

 ShipName,        68

 ShipAddress,     92

 ShipCity,        30

 ShipRegion,      26

 */

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.