The following two statements both have negligible execution times over a relatively large table. Subsequently I was wondering which would be deemed better practice? Personally I think that the ‘case’ (option 2) statement reads a lot easier to somebody who make look at this a year from now however option 1 seems a lot cleaner.
Option #1:
select *
from imaginarytable
where 1=1
and (@section_name = 'ALL' or upper(section_name) = upper(@section_name))
Option #2:
select *
from imaginarytable
where 1=1
and upper(section_name)=
case when @section_name <> 'ALL'
then upper(@section_name
else upper(section_name)
end
Personally, I cannot imagine anyone finding the
CASEversion more clear. The first option is basic boolean logic, which every CS student learns and with which we all should find very comfortable.Plus, I’m not even sure your 2 expressions are equivalent, in part because the
CASEexpression is basically incomprehensible.