Khairy Abd el-Zaher Khairy Abd el-Zaher - 1 year ago 68
SQL Question

SQL Server Select with different where conditions for different field values

I want to select data from my table

which has a field called
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 Source
select * from offer
where (type =1 and condition_1 )
or ( type = 2 and condition_2 and condition_3 ) 
or (type = 3 and condition_3 ) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download