Search This Blog

Thursday, March 21, 2013

DATALENGTH SQL Server

SQL Server > Built-In Functions > DATALENGTH

Returns the number of bytes used to represent any expression
Example:

IF EXISTS(SELECT name FROM sys.tables
      WHERE name = 'test')
   DROP TABLE test;
GO

CREATE TABLE test
(
 c1 varchar(5),
 c2 char(5),
 c3 nvarchar(5)
);
GO

INSERT INTO test VALUES ('1', '1','1'), ('22', '22', '22'),('333', '333', '333');
GO
SELECT 
    DATALENGTH(c1) AS 'Varchar Column',
    DATALENGTH(c2) AS 'Char Column',
    DATALENGTH(c3) AS 'NVarChar Column'
FROM
    test;
GO

Result:

Varchar Column      Char Column  NVarChar Column
1                   5             2
2                   5             4
3                   5             6