Why does this work…
DECLARE @MyInt int = 12345;
SELECT * FROM MyTable WHERE MyId = @MyInt; --Returns 1 row
SELECT * FROM MyTable WHERE MyId = 12345; --Returns 1 row
but this doesn’t?
DECLARE @MyVarchar varchar = 'ABCDEF';
SELECT * FROM MyTable WHERE MyId = @MyVarchar; --Returns 0 rows
SELECT * FROM MyTable WHERE MyId = 'ABCDEF'; --Returns 1 row
SQL Server version is 10.50.1746
Because when you
declare, defaultvarcharlength is1. So@MyVarcharends up being'A'.This is different to
cast(something as varchar), where default length is30.The right thing is
where
10is the length of the column in the table.