Engineering scalable systems, cloud-native applications, and AI-powered solutions.

How to check number of bytes used by a variable in SQL Server?

To check bytes used by a variable in SQL Server, you can use DATALENGTH function.

Ankush Jain

Ankush Jain

Lead Software Engineer (VP) | Software Architect

Published Nov 12, 2018Updated Nov 24, 2026

To check bytes used by a variable in SQL Server, you can use DATALENGTH function.

sql
DECLARE @temp NVARCHAR(MAX)
SET @temp = N'化字'
SELECT DATALENGTH(@temp)

This will display the number of bytes used @temp variable to represent a character or expression.

Have feedback or thoughts?

Discuss this article or share it on Twitter / GitHub.