Tuesday, April 25, 2017

DATALENGTH() in SQL Server


SQL DATALENGTH() function is used to calculate the number of bytes required to represent an expression. It returns the number of character in a string. DATALENGTH() works on all types of data types. NULL values are not considered.

Syntax:

     
            SELECT DATALENGTH(expression)
           

expression is the any data type of which data length is to be calculated.
  



Return Types:

DATALENGTH() returns bigint if expression specified is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, int is returned.


Example 1:
DATALENGTH() on numeric column.

SELECT * FROM TempEmployeePayHistory



SELECT Rate, DATALENGTH(Rate) FROM TempEmployeePayHistory



Example 2:
DATALENGTH() on column with NULL values.

          SELECT * FROM TempEmployeePayHistory



SELECT Rate, DATALENGTH(Rate) FROM TempEmployeePayHistory



Example 3:
DATALENGTH() on text column.

          SELECT * FROM DatabaseLog



          SELECT DISTINCT [Event], DATALENGTH([Event]) FROM DatabaseLog




Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.

0 comments: