4

I'm using SQL Server, how do I use a CASE statement within a where clause in a SQL statement?

I want to rewrite this query:

select * 
from Persons P
where P.Age = 20
  and P.FamilyName in (select Name
                       from AnotherTable)

Using a case statement. I want it so the second condition

(P.FamilyName in ....)

is executed only if CheckFamilyName is true.

Something like this:

select * 
from Persons P
where P.Age = 20
case when CheckFamilyName= true 
 then
    and P.FamilyName in (
              select Name
              From AnotherTable)
 else 
end

How do case statements in SQL work?

1
  • First thing : CASE in T-SQL is an expression (like a+b) - NOT a statement. CASE can return a single, atomic value - it cannot be used to conditionally execute code fragments or code paths.
    – marc_s
    Dec 29, 2015 at 16:57

3 Answers 3

4
where   P.Age = 20
        and 
        (
            not CheckFamilyName
            or
            P.FamilyName in (select Name From AnotherTable)
        )
2

approach 1

select * 
from Persons P
where 1=1
    and P.Age = 20
    and (
        CheckFamilyName = 0
        or P.FamilyName in (select Name From AnotherTable)
    )

approach 2

select * 
from Persons P
where 1=1
    and P.Age = 20
    and CheckFamilyName = '0'
union all
select * 
from Persons P
where 1=1
    and P.Age = 20
    and CheckFamilyName = '1'
    and P.FamilyName in (select Name From AnotherTable)
9
  • If CheckFamilyName is false, this would still check family name?
    – Andomar
    Dec 29, 2015 at 13:59
  • Why do you add the 1=1 in both approaches? Dec 29, 2015 at 14:04
  • it's a syntactic sugar, does not cost any processing power, and can be easily commented out.
    – armen
    Dec 29, 2015 at 14:06
  • Based on this definition I don't see how it makes anything easier to read or express. Merely adds unnecessary keystrokes. Dec 29, 2015 at 14:08
  • 1
    @MichaelMcGriff - it is just one of those dumb things people do to make copy and paste easier -- like putting commas at the start of the line. This way every line you would copy and paste is always the "same". I've never seen it be much help and countless times it has introduced bugs. This is not "syntatic sugar" it is coding style IMO and a silly one at that.
    – Hogan
    Dec 29, 2015 at 14:27
0

First of all you can (and probably should) use a join for your first case like this:

select * 
from Persons P
join anothertable A on A.Name = P.FamilyName
where P.Age = 20

It should be noted that this select statement will ONLY give you results with that family name.

So when you add in your new condition

select * 
from Persons P
join anothertable A on A.Name = P.FamilyName and P.CheckFamilyName= true 
where P.Age = 20

You will only get results where CheckFamilyName is true.

So we make it a left join.

select * 
from Persons P
left join anothertable A on A.Name = P.FamilyName and CheckFamilyName= true 
where P.Age = 20

This will include rows where checkfamilyname is true but name does not exist -- to validate those you can do the following

select * 
from Persons P
left join anothertable A on A.Name = P.FamilyName and CheckFamilyName= true 
where P.Age = 20 and (checkfamilyname = A.Name is not null)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.