The query below runs fine, although as tdatgod asked, why would you not qualify on the attributes within the Case statement themself?
SELECT CountiesV.Name, CountiesV.CountyCode
FROM CountiesV
WHERE (Case When DHSViews.CountiesV.CountyCode > '027' Then 'Rural' Else 'Metro' End) = 'metro' ;
SELECT CountiesV.Name, CountiesV.CountyCode
FROM CountiesV
WHERE (Case When DHSViews.CountiesV.CountyCode > '027' Then 'Rural' Else 'Metro' End) = 'metro' ;
But how does this differ from....
SELECT CountiesV.Name, CountiesV.CountyCode
FROM CountiesV
WHERE
(DHSViews.CountiesV.CountyCode <= '027');
Basically every CASE statement could simply be coverted to another condition in the where clause all put together with "AND"s and "OR"s.
what does the Case statement do that the simple where clause doesn't?
The original question was can it be done, and an example, which I provided.
The one reason I can think of using a case statement in a where clause, is to help business users with coding. For example if metro counties are 001,005,027, 082,097, the user would enter 'metro' instead of having to know source codes. If it is a non-indexed attribute, I don't think any efficiency is lost because both are full table scans.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.