Khairy Abd el-Zaher Khairy Abd el-Zaher - 1 month ago 8
SQL Question

SQL Server Select with different where conditions for different field values

I want to select data from my table

offer
which has a field called
type
and I need to apply different conditions to each type.

The code may look like this:

select * from offer where
if type = 1 then apply condition_1,
else if type = 2 then apply condition_2 and condition_3,
else if type = 3 then apply condition_4,


So how can I achieve that?

Answer
select * from offer
where (type =1 and condition_1 )
or ( type = 2 and condition_2 and condition_3 ) 
or (type = 3 and condition_3 )