It seems that LEN() ignores whitespaces at the right side of a variable.
declare @a varchar(100)
set @a = 'John '
print len(@a)
The above code prints 4 whereas it should be 7.
Is this a bug?
CodePudding user response:
This is not a bug, this is the intended behavior. To quote the documentation:
LEN
excludes trailing spaces. If that is a problem, consider using theDATALENGTH
(Transact-SQL) function which does not trim the string
CodePudding user response:
Not a bug, is right there in the documentation:
Returns the number of characters of the specified string expression, excluding trailing spaces.
CodePudding user response:
Please read the official documentation. It is expected behaviour. https://docs.microsoft.com/en-us/sql/t-sql/functions/len-transact-sql?view=sql-server-ver15#remarks