how to implement case T-SQL in where
How can I implement the case selection operator in the where condition?
You need to do the following:
the stored procedure accepts @type int
select * from table as t
where case @type when 1 then t.type = 1
when 2 then t.type != 1
when 3 then t.type = t.type
Ms sql server 2012
2
Author: aleksandr barakin, 2016-01-18
2 answers
declare @t table ([type] int)
declare @type int
insert into @t ([type])
values (0), (1), (2), (3), (4)
set @type = 3
select * from @t as t
where @type = case
when @type = 3 then 3
when t.type = 1 then 1
when t.type <> 1 then 2
END
1
Author: Konst, 2016-01-18 13:57:50
select * from table as t
where ( (@type=1 and t.type=1)
or (@type=2 and t.type!=1) )
1
Author: Mike, 2016-01-18 13:42:30