0

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.: enter image description here 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
5
  • 1
    What's the problem with your query? Do you got any error? unexpected results?? Oct 8, 2012 at 12:43
  • the query runs, but with the second case statement, the else doesn't return the original value of what was in the row, everything is 'countyName' Oct 8, 2012 at 12:44
  • @MahmoudGamal screen cap was edited in. Oct 8, 2012 at 12:47
  • Then you don't have a value of '' in that field, you have a value of NULL in that field. Try one of the following... COALESCE(countyName, 'Statewide') or CASE WHEN countyName IS NULL OR countyName = '' THEN 'Statewide' ELSE countyName END.
    – MatBailie
    Oct 8, 2012 at 12:47
  • 1
    Have you checked whether the countyName is blank or is null ?
    – Anish V
    Oct 8, 2012 at 12:48

5 Answers 5

5

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...

,case countyName
when '' then 'Statewide'
else countyName end

As an extra safety net, you can also check for NULL values using ISNULL...

,case ISNULL(countyName,'')
when '' then 'Statewide'
else countyName end
1
  • Not a problem @woot, glad to have helped. Good luck with the rest of your project
    – freefaller
    Oct 8, 2012 at 12:53
1

Try this:

case when isnull(countyName,'') = '' then 'Statewide' else countyname end
0

I guess you are looking for the "IFNULL" function.

simply use it this way: SELECT ...,ISNULL( column, "fallback value" ),... in your case

1
  • 1
    supid me, marc_s is right. Getting old and confused by all the sql-derivates.
    – Najzero
    Oct 8, 2012 at 12:47
0

Try this... IsNULL should be checked.

case ISNULL(countyName,'') when '' then 'Statewide' else 'countyname' end

0
SELECT   ProductNumber, Category =
      CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         WHEN 'T' THEN 'Touring'
         WHEN 'S' THEN 'Other sale items'
         ELSE 'Not for sale'
      END,
   Name
FROM Production.Product
ORDER BY ProductNumber;

GO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.