Skip to content
Advertisement

SQL SERVER: Check if variable is null and then assign statement for Where Clause

I am trying to achieve something like the below in WHERE clause in sql.

I tried the following:

which is wrong. Can anyone help in framing the exact statement. Thanks!

Advertisement

Answer

Isnull() syntax is built in for this kind of thing.

For your example keep in mind you can change scope to be yet another where predicate off of a different variable for complex boolean logic. Only caveat is you need to cast it differently if you need to examine for a different data type. So if I add another row but wish to specify int of 8 AND also the reference of text similar to ‘repeat’ I can do that with a reference again back to the ‘isnull’ of the first variable yet return an entirely different result data type for a different reference to a different field.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement