Hi, we faced very strange issue (really strange for such mature product): how to get number of characters in unicode string using Transact-SQL statements. The key problem of this issue that the len() TSQL function returns number of chars, excluding trailing blanks. the other variant is to use datalength (which return number of bytes) and divide by 2, so get numbers of unicode chars. But Unicode chars can be surrogate pairs so its wont work either.
We have 2 variants of solution, first is use len(replace()) and second is add single symbol and then substract 1 from result. But imo both variants are rather ugly.
declare @txt nvarchar(10)
set @txt = 'stack '
select @txt as variable,
len(@txt) as lenBehaviour,
DATALENGTH(@txt)/2 as datalengthBehaviour,
len(replace(@txt,' ','O')) as ReplaceBehaviour,
len(@txt+'.')-1 as addAndMinusBehaviour
Any other ideas how to count chars in string with traililng spaces ?