Question

I am having trouble understanding why the RIGHT function is not working for me. I tried to fit as much output in here, my apologies if it is confusing to read.

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'

Number: 154448887859999
LEFT: 1544
Length: 15
SUBSTRING: 9999
RIGHT: EMPTY

Was it helpful?

Solution

spaces perhaps, LEN doesn't count spaces while DATALENGTH does

can you run

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

OTHER TIPS

try

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'

I found this to be quite revealing.

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 + ''''

yields the following output

(No column name)

1 3

(No column name)

1 '5am '

(No column name)

1 4

(No column name)

1 '06am'

(No column name)

1 1

(No column name)

1 '0'

See for yourself

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top