Open In App

DATALENGTH() Function in SQL Server

Last Updated : 03 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

DATALENGTH() :
This function returns the length of an expression and expression length will be in bytes.

Syntax :

DATALENGTH( expression )  
  • Parameter –
    This method accepts a single-parameter as mentioned above and described below as follows.
  • Expression –
    It is an expression of the data type to return the length for. If expression is NULL, it returns NULL.

Returns :
It returns the number of bytes used to represent an expression.

Example-1 :
Return the length of an expression.

SELECT DATALENGTH('Geeksforgeeks');

Output :

13

Example-2 :
Check DATALENGTH() Function count trailing spaces or not.

SELECT DATALENGTH('GFG   ');

Output :

6

Example-3 :
Check DATALENGTH() Function count leading spaces or not.

SELECT DATALENGTH('    GFG');

Output :

7

Example-4 :
DATALENGTH() Function return NULL, if the expression is NULL.

SELECT DATALENGTH(NULL);

Output :

NULL

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads