Home > Software engineering >  does LEN() have bug?
does LEN() have bug?

Time:12-25

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 the DATALENGTH (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

  • Related