SQL Flow Field Guide

DATALENGTH

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

type or identity metadata

What changes

Input
argument expression or table identity metadata
Output or change
value
Return type
bigint if expression has an nvarchar(max), varbinary(max), or varchar(max) data type; otherwise int.

Syntax at a glance

DATALENGTH ( expression )

Work example

Check the storage byte count of a value or detect padding differences.

Compare values of different encodings with their declared type in mind.

Check your work DATALENGTH returns bytes, while LEN returns characters and omits trailing spaces.

Practice SQL

Open in SQL Flow
SELECT LEN(N'Hi ') AS character_count, DATALENGTH(N'Hi ') AS byte_count;

Open Microsoft Learn for full syntax, permissions, and details for your version.