I have a single table with no relations. When the field countyName is a zero length string I need Statewide to be in the result set. Otherwise, I need then value of the field to stay as it is, e.g.:
notice how in the second column there is ‘Statewide’ and ‘countyName’. countyName should be the original value actually stored in the database.
`countyName` `address`
blah blah
Jackson blah blah
needs to be (first rows on both examples are column names)
countyName address
Statewide blah blah
Jackson blah blah
Here’s what I tried, you can ignore the rest of the fields in this example
select case servicetype
when 'cr' then 'Community Resource'
when 'ed' then 'Education'
when 'fb' then 'Faith-based'
when 'me' then 'Medical Equipment'
when 'hc' then 'Health Care'
else 'Other'
end as serviceType
,case countyName
when '' then 'Statewide'
else 'countyname' end
,name
,physicaladdress
,city
,statelocation
,zip
,phone
,website
from main
order by countyName, servicetype, name
Despite not saying exactly what results you’re expect, my guess would be that you don’t want to return the literal of ‘countyname’. What you want is the value in the column.
So try replace the current case statement with…
As an extra safety net, you can also check for
NULLvalues usingISNULL…