DBA > Job Interview Questions > Microsoft SQL Server FAQs

Can you use a CASE statement in a WHERE clause?

More DBA job interview questions and answers at http://dba.fyicenter.com/Interview-Questions/

(Continued from previous question...)

Can you use a CASE statement in a WHERE clause?

Answer: Yes

Explanation: You can. Try this code:
create table MyTable
( mydata int
, mycompare1 char(1)
, mycompare2 char(1)
)
go
insert MyTable select 1, 'A', 'B'
insert MyTable select 2, 'A', 'C'
insert MyTable select 3, 'D', 'G'
insert MyTable select 4, 'E', 'B'
GO
select *
from MyTable
where 1 = CASE
WHEN mycompare1 = 'A' then 1
ELSE 0
END
GO
drop table MyTable

(Continued on next question...)

Other Job Interview Questions