question

asmasm avatar image
asmasm asked

Select case in sql server

Dear all Please let me know why i'm not getting same results in below cases how to achieve without third variable declare @TaxName VARCHAR(30)=NULL, @StateCode INT=0 select @TaxName=NULL SELECT TaxId,TaxName,TaxPercent,StateCode from Taxes WHERE Active=1 and TaxName = ( case when TaxName =NULL then TaxName else @TaxName end) go SELECT TaxId,TaxName,TaxPercent,StateCode from Taxes WHERE Active=1 and TaxName = TaxName It should give same output.
selectcase-statement
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
ThomasRushton avatar image
ThomasRushton answered
NULL. Don't use = when looking for NULL values, as NULL isn't a value you can compare (even with NULL)... Use, for example, CASE WHEN TaxName IS NULL instead of CASE WHEN TaxName = NULL Have a look at Robert Sheldon's Simple-Talk article "[How to Get NULLs Horribly Wrong in SQL Server][1]" [1]: https://www.simple-talk.com/sql/t-sql-programming/how-to-get-nulls-horribly-wrong-in-sql-server/
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

asmasm avatar image asmasm commented ·
Oh my bad. :)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.