My colleague created this SQL to handle certain user selections which are based on an on/off bit flag. The goal is to identify those individual bit flags with a specific integer which will be used in a later SQL to group specific types which the flags relate to.
What would be a more elegant way of doing this on SQL-Server 2012 which would remove the proceeding if statements?
DECLARE @ct1 int;
DECLARE @ct2 int;
DECLARE @ct3 int;
DECLARE @ct4 int;
SELECT @ct1 = ISNULL(TypeDaily, 0) ,
@ct2 = ISNULL(TypeTerm, 0) ,
@ct3 = ISNULL(TypePerformance, 0) ,
@ct4 = ISNULL(TypeWeather, 0)
FROM ReportTable
WHERE ReportSelectionId = @ReportSelectionId;
if (@ct2 > 0)
select @ct2 = 2
if (@ct3 > 0)
select @ct3 = 3
if (@ct4 > 0)
select @ct4 = 4
The Type....
fields above are valid bit columns on the ReportTable
table.