0

My string contains leading and trailing spaces (see below).

When I applied the LRTIM function it appears to work. Next, when I nest the LTRIM function with the LEN function to the string, it appears the length is off by 1 (i.e. length of original string is 16, LTRIM removed the single (1) leading space from the string, so I expected LEN to return a length of 15, not 14). Any explanations?

SELECT
 ' This is a test ' AS origStr
,LEN(' This is a test ') AS origStrLen
,'[' + LTRIM(' This is a test ') + ']' AS ltrimStr
,LEN(LTRIM(' This is a test ')) AS strLtrimLen
;

Results:

This is a test | 16 | [This is a test ] | 14 |
  • Sorry for the re-posted of this specific question. And thanks for providing the link that explains this issue. I read up on DATALENGTH vs LEN. – user3033344 Nov 25 '13 at 17:59

1 Answers1

0

It is because the len() not counting after string space.

LEN(LTRIM(' This is a test ')) has one space before and one space after string. 

Example: SELECT LEN('TEXT ') RETURNS---> 4

Amir Keshavarz
  • 3,050
  • 1
  • 19
  • 26