开发者

Transact Sql LEFT function weird output

开发者 https://www.devze.com 2023-01-08 14:21 出处:网络
select replace(stuff(\'123456\',2,2,\'ABCD\'),\'1\',\' \') select LEFT(\'ABCD456\',4) select left(replace(stuff(\'123456\',2,2,\'ABCD\'),\'1\',\' \'),4)
select replace(stuff('123456',2,2,'ABCD'),'1',' ')

select LEFT('ABCD456',4)

select left(replace(stuff('123456',2,2,'ABCD'),'1',' '),4)

Ok now the first select outputs 'ABCD456', the series of functions e开发者_StackOverflow社区valuates to that exactly the first parameter to the left function in the second select second select returns 'ABCD' as expected third select returns 'ABC'

Why? Shouldn't it also output 'ABCD'? Does anybody know? Thanks in advance.


It is clearer if you do

select '[' + left(replace(stuff('123456',2,2,'ABCD'),'1',' '),4) + ']'

which returns

[ ABC]

There is a leading space!

select stuff('123456',2,2,'ABCD') Gives 1ABCD456

Then you replace the 1 with a space

0

精彩评论

暂无评论...
验证码 换一张
取 消