TSQL RIGHT字符串函数不起作用

时间:2013-10-08 14:49:02

标签: sql-server tsql varchar

我无法理解为什么RIGHT功能对我不起作用。我试着在这里输入尽可能多的输出,如果它让人感到困惑,我会道歉。

DECLARE @Nbr VARCHAR(27)

SELECT @Nbr = xmz.nbr
FROM @xml_temp AS xmz

SELECT @Nbr AS 'Number', 
       LEFT(@Nbr, 4) AS 'LEFT', 
       LEN(@Nbr) AS 'Length', 
       SUBSTRING(@Nbr, 10, 4) AS 'SUBSTRING', 
       RIGHT(@Nbr, 4) AS 'RIGHT'

编号:154448887859999
左:1544
长度:15
SUBSTRING:9999
右: EMPTY

3 个答案:

答案 0 :(得分:7)

或许

空格,LEN在DATALENGTH

时不计算空格 你能跑吗

SELECT DATALENGTH(@Nbr) AS 'Length',
 RIGHT(RTRIM(@Nbr), 4) AS 'RIGHT'

答案 1 :(得分:0)

SELECT @Nbr AS 'Number', 
       LEFT(@Nbr, 4) AS 'LEFT', 
       LEN(@Nbr) AS 'Length', 
       SUBSTRING(@Nbr, 10, 4) AS 'SUBSTRING', 
       RIGHT(RTRIM(@Nbr), 4) AS 'RIGHT'

答案 2 :(得分:0)

我发现这很有启发性。

DECLARE @from char(4) = '5am';
DECLARE @to varchar(4) = '6am';
DECLARE @not_to_be varchar = '7am';
SET @from = RIGHT('0' + @from,4)
SELECT LEN(@from)
SELECT '''' + @from + ''''
SET @to = RIGHT('0' + @to,4)
SELECT LEN(@to)
SELECT '''' + @to + ''''
SET @not_to_be = RIGHT('0' + @not_to_be,4)
SELECT LEN(@not_to_be)
SELECT '''' + @not_to_be + ''''

产生以下输出

(No column name)

1 3

(No column name)

1'5am'

(No column name)

1 4

(No column name)

1'06 am'

(No column name)

1 1

(No column name)

1'0'

参见yourself