sql server 2014 - T-SQL CASE in the WHERE Clause using BETWEEN dates -
having problem syntax, need return result based on number , date range, if 6,2 date range else other date range.
declare @test table ( id nvarchar(10) ,number int ,[date] datetime ) insert @test values ('tg32',6,'20160715') insert @test values ('tg47',6,'20160803') insert @test values ('ag9',6,'20160805') insert @test values ('bf27',2,'20160804') insert @test values ('qd65',2,'20160802') insert @test values ('f98',0,'20160806') insert @test values ('gc5',0,'20160731') insert @test values ('ht76',0,'20160802') insert @test values ('kl81',0,'20160805') insert @test values ('wr52',0,'20160802'); select top 10 * @test [date] = case when number in ('6','2') ([date] between cast(getdate() - 6 date) , getdate()) else ([date] between cast(getdate() - 1 date) , getdate()) end
you can't use case
in way, in sql case not if else
more similar function (test?if_yes;if_not)
where [date] between cast(getdate() - case when number in ('6','2') 6 else 0 end date) , getdate()
Comments
Post a Comment