I was wondering if someone can explain this behaviour?
DECLARE @RandomParam1 NVARCHAR
DECLARE @RandomParam2 NVARCHAR
DECLARE @RandomParam3 NVARCHAR
SET @RandomParam1 = '0HelloWorld'
SET @RandomParam2 = '9HelloWorld'
SET @RandomParam3 = '15HelloWorld'
select 1 where '0' = @RandomParam1 -- true
select 1 where '0' = '0HelloWorld' -- false
select 1 where '9' = @RandomParam2 -- true
select 1 where '15' = @RandomParam3 -- false
Why does a string comparison with parameters yield a different result than without parameters? And why does it claim that ‘0’ = ‘0whatever’?
I get that it may be that the parameter tries to compare it as numbers, but then the last example should be true aswell.
Any ideas?
The default length for
NVARCHARis 1. Your three parameters effectively all contain just one single character.If you change your declarations to
you will get the behavior you were expecting.
nchar and nvarchar