Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

I (didn't) find a BUG with the T-SQL Right () function :-þ

$
0
0

I have discovered a bug that exists in both SQL 2008 SP2 and SQL 2012 SP1 T-SQL.

SELECT '000' + cast(  cast('010' as int)-1 as char(3) ) AS NewSeq  -- Correctly Results in 0009

SELECT RIGHT('000' + cast(  cast('010' as int)-1 as char(3) ), 3) AS NewSeq   -- INCORRECTLY Results in 9.  It should be 009
SELECT RIGHT('000' + Convert(char(3),  cast('010' as int)-1   ), 3) AS NewSeq  -- INCORRECTLY Results in 9. It should be 009

SELECT SubString( '000' + cast(  cast('010' as int)-1 as char(3) ), 2, 3 ) AS NewSeq  -- Correctly Results in 009
SELECT SubString( '000' + Convert(char(3),  cast('010' as int)-1   ), 2, 3 ) AS NewSeq  -- Correctly Results in 009


SELECT SubString(RTRIM(CAST('000' + Convert(char(3),  cast('010' as int)-1   ) as char(6))), 2, 3) AS NewSeq  -- Correctly Results in 009
SELECT RIGHT(    RTRIM(CAST('000' + Convert(char(3),  cast('010' as int)-1   ) as char(6))), 3) AS NewSeq  -- CORRECTLY Results in 009

SELECT RIGHT('000' + '009', 3) AS NewSeq -- Correctly Results in 009



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>