1) If you're not concerned about excluding nulls from your count, you don't need to specify a column name in your COUNT statement. i.e.
select count(Column1)
Will give the same result as
select count(1)
Or
select count(*)
So long as column1 has no null values. If column1 does contain nulls, those aren't counted (so if there are 10 records, 3 of which have null values in column1, you'd get a result of 7 using count(column1)
or a count of 10 using the other methods.
I mention this first as if you care about nulls then changing which column is used here makes sense; if you don't, go with the simpler logic of just count(1)
/ counnt(*)
.
All that said, here's how to change that column:
select count(
case @Region
when 1 then Column1
when 2 then Column2
else Column3
end
)
2) If you want to change the column used in your WHERE statement, there are a couple of approaches:
SELECT COUNT(1)
FROM bankholidays
WHERE case @Region
when 1 then Column1
when 2 then Column2
else Column3
end BETWEEN @StartDate AND @EndDate
or
SELECT COUNT(1)
FROM bankholidays
WHERE (@Region = 1 and Column1 BETWEEN @StartDate AND @EndDate)
or (@Region = 2 and Column2 BETWEEN @StartDate AND @EndDate
or (@Region not in (1,2) and Column3 BETWEEN @StartDate AND @EndDate
Personally I prefer the first style above, since it involves less repetition; however the second style offers the option to use different start & end dates for the different columns, or to add in other logic too, so is still worth being aware of.