Solved

Nested case statement

Posted on 2014-02-05
5
381 Views
Last Modified: 2014-02-05
Hi experts,

I am  trying to write a nested case statement and I am getting an error.  Please see a sample of what I am trying to do below:

Case when dbo.field1 in ('val1','val2') then 'test1'
When dbo.field1= 'val3' then 'test2'
Else 'blah'
End Newfieldname

Then I need to use the 'Newfieldname' in another case statement to create another field.

Case when 'Newfieldname'  in ('test1','test2') then 'Yes'
End Newfieldname


How can I get this done?
0
Comment
Question by:daintysally
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39836115
You have to use something like tghis

SELECT  Case when 'Newfieldname'  in ('test1','test2') then 'Yes'
End Newfieldname

       
FROM (
SELECT
Case when dbo.field1 in ('val1','val2') then 'test1'
When dbo.field1= 'val3' then 'test2'
Else 'blah'
End Newfieldname
FROM TableA )
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39836116
You either need to duplicate the first case statement within the second case statement or you need to include the first case statement in a subquery.

CASE
   WHEN
      CASE
         WHEN dbo.field1 in ('val1','val2') then 'test1'
         WHEN dbo.field1= 'val3' then 'test2'
         ELSE 'blah'
      END IN 'test1', 'test2' THEN 'YES'
   ELSE 'No'
END AS Newfieldname
0
 

Author Comment

by:daintysally
ID: 39836177
I tried BriCrowe 's solution and there is an error on the first case statement.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39836187
sry...air code.  I forgot parentheses around the IN values

CASE
   WHEN
      CASE
         WHEN dbo.field1 in ('val1','val2') then 'test1'
         WHEN dbo.field1= 'val3' then 'test2'
         ELSE 'blah'
      END IN ('test1', 'test2') THEN 'YES'
   ELSE 'No'
END AS Newfieldname
0
 

Author Closing Comment

by:daintysally
ID: 39836206
This worked great!!  Thank you!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question