Tip of the Day : Similarities and Differences Between LEN and DATALENGTH Functions

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - August 17, 2018

Similarities and Differences Between LEN and DATALENGTH Functions

The LEN system function returns the number of characters of the specified string expression.  On the other hand, the DATALENGTH function returns the number of bytes used to represent any expression.  Here’s a summary of the similarities and differences between the LEN() and DATALENGTH() system functions:

Similarities

  • The LEN and DATALENGTH of NULL is NULL.
  • The return data type is BIGINT if the input expressions is of the VARCHAR(MAX), NVARCHAR(MAX) or VARBINARY(MAX) data types; otherwise the return data type is INT.

Differences

LEN DATALENGTH
Returns the number of characters of the specified string expression. Returns the number of bytes used to represent any expression.
Excludes trailing blanks in determining the number of characters. Includes trailing blanks in determining the number of bytes used.
Input parameter is of character or binary data type. Input parameter is of any data type.
LEN is a string function. DATALENGTH is a data type function.

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions