Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL check in Sql

Tags:

sql

sql-server

Please suggest whats the difference between:

1) WHERE student.name = ISNULL(@name, student.name)

And

2) WHERE (student.name = @name OR @name IS NULL)

Actually I had a issue bug assigned against my name where some of the records where skipped when I used the first method. But got corrected when I replaced it with eg 2.

like image 421
Justin Samuel Avatar asked Aug 27 '26 02:08

Justin Samuel


1 Answers

The second parameter for ISNULL() is a replacement value, meaning the value that will be substituted if the first parameter is null. Thus, if both @name and student.name were null, you'd essentially be writing:

student.name = NULL

Which equates to false (as null in SQL is considered unknown, and never equals anything).

In the second, you're using the IS operator to test for a null value, which will return true if @name is null.

like image 90
Mike Christensen Avatar answered Aug 28 '26 17:08

Mike Christensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!