带有前导和尾随空格的tsql LTRIM和LEN

时间:2013-11-25 17:36:25

标签: sql-server tsql

我的字符串包含前导和尾随空格(见下文)。

当我应用LRTIM功能时,它似乎工作。接下来,当我将带有LEN函数的LTRIM函数嵌套到字符串时,看起来长度偏离1(即原始字符串的长度为16,LTRIM从字符串中删除了单个(1)前导空格,所以我期待LEN返回15的长度,而不是14)。有什么解释吗?

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
;

结果:

This is a test | 16 | [This is a test ] | 14 |

1 个答案:

答案 0 :(得分:0)

这是因为len()在字符串空格后不计算。

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

实施例: SELECT LEN('TEXT ')退货---> 4