I want to count the characters in an ntext field. Following Pinal Dave's advice, I am using datalength. But this function seems to double the value I am looking for. When I copy the value in the field into word and count the characters, I get 1502. But when I do
select datalength(result) from myTable
I get a value of 3004 characters.
Why?
Unicode is two bytes per character. Your NText
field is a Unicode string. DataLength()
returns the number of bytes required to store a field, Len()
returns the number of characters.
DataLength
by two to get the number of characters. You can divide by DataLength( N'X' )
as a hint/reminder - HABO 2012-04-04 21:32