The DATALENGTH function returns the number of bytes in a given expression.
If NULL is provided, this function returns NULL as well.
DATALENGTH returns the # of bytes, whereas LEN returns the # of characters.
This example return the number of bytes of the input string.
SELECT DATALENGTH('Austin, Texas') AS 'Data Length'
| Data Length |
|---|
| 13 |
Syntax of the DATALENGTH function.
DATALENGTH(expression)
expression -- a value of any data type, or column name.
| CUSTOMER |
|---|
| Id |
| FirstName |
| LastName |
| City |
| Country |
| Phone |
SELECT FirstName,
DATALENGTH(FirstName) AS Bytes
FROM Customer
| FirstName | Bytes |
|---|---|
| Paolo | 10 |
| Pedro | 10 |
| Marias | 12 |
| Kyle | 8 |
| Miguel | 12 |
![]() |
|
Note: FirstName is a NVARCHAR string, so each character is 2 bytes.