Skip to content

How to consider trailing spaces

Sometimes it is neccessary to take any character into account. The standard behavior of sql SQL Server LEN function ignores trailing spaces.

so if you try this

    SELECT LEN(' 234 ') length

it returns 4

If you need all characters, even the trailing spaces. You have to replace them BEFORE you get the length via LEN funtion.

DECLARE
 @VARCHAR6 VARCHAR(6) = ' 1234 '

select len(@VARCHAR6) _Len,  len(REPLACE(@VARCHAR6, ' ', '#')) _LenWithSpaceReplace,  datalength(@VARCHAR6) _DataLength

I hope it helps.

Published inUncategorized

Comments are closed.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close