When using T-SQL in SQL Server (or Azure) the LEN()
and DATALENGTH()
functions will often return the same result, but not always. There are some cases where these functions will return completely different results for what appears to be the same data. This is because there’s an important difference between how the LEN()
and DATALENGTH()
functions work, as we’ll see here.
First up, here’s a quick definition of each:
LEN()
- Returns the number of characters of the specified string expression, excluding trailing blanks.
DATALENGTH()
- Returns the number of bytes used to represent any expression.
Note “characters” vs “bytes”. Also note that “excluding trailing blanks” only applies to one.
Here are some examples to demonstrate the differences between LEN()
and DATALENGTH()
.
Continue reading