DATALENGTH (Transact-SQL)
Returns the number of bytes used to represent any expression.
DATALENGTH is especially useful with varchar, varbinary, text, image, nvarchar, and ntext data types because these data types can store variable-length data.
The DATALENGTH of NULL is NULL.
Note
|
|---|
|
Compatibility levels can affect return values. For more information about compatibility levels, see ALTER DATABASE Compatibility Level (Transact-SQL). |
LEN vs DATALENGTH
Use DATALENGTH when the actual number of bytes is required, including trailing spaces. So, in a double-byte encoding, you'll get actual bytes, not characters. Use LEN if trailing spaces should not be counted and character count is required, independent of the byte encoding used.
- 1/28/2011
- RogueCoder
