SQL Where question

Posted by needshelp on Stack Overflow See other posts from Stack Overflow or by needshelp
Published on 2010-05-11T00:05:07Z Indexed on 2010/05/11 0:14 UTC
Read the original article Hit count: 571

Filed under:
|

Hi all,

I have a question about case statements and nulls in a where clause.

I want to do the following:

Declare @someVar int = null

select column1 from
TestTable t
where t = case when @someVar is not null then @someVar else t end

Here is the problem:

Let's say @someVar is null. Let's also say that column1 from TestTable t has NULL column values. Then, my condition t = t in the case statement will always evaluate to false.

I basically just want to be able to conditionally filter the column based on the value of @someVar if it's provided. Any help?

© Stack Overflow or respective owner

Related posts about sql

Related posts about null