SQL server IF begin statement in where function -
i'm trying create following in statement.
registration_date >=@startdate , registration_date < dateadd(day,1,@enddate) , if @affiliateid null begin channel in (select value dbo.split(',',@channel)) end else affiliate_id in (select value dbo.split(',', @affiliateid)) , registration_channel in (select value dbo.split(',', @channel))
so, trying if @affiliateid null, use @channel input affiliate ids, if not use specific @affiliateid, irrespective of channels.
is there way make work?
select * mytable date(registration_date) between @startdate , @enddate , exists ( select null dbo.split(',', coalesce(@affiliateid, @channel)) value = case when @affiliateid null channel else affiliateid end ) , exists ( select null dbo.split(',', @channel) value = registration_channel )
in sql server 2008
, date(column)
sargable expression.