Key in the question: use DATALENGTH() . Note that this has a different behavior for LEN() :
SELECT LEN(CAST('Hello ' AS NVARCHAR(MAX))), DATALENGTH(CAST('Hello ' AS NVARCHAR(MAX))), DATALENGTH(CAST('Hello ' AS NTEXT))
returns 5, 16, 16.
In other words, DATALENGTH() does not remove trailing spaces and returns the number of bytes, while LEN() trims trailing spaces and returns the number of characters.
onedaywhen Oct 24 '08 at 8:27 2008-10-24 08:27
source share