Solved

Nested case statement

Posted on 2014-02-05
5
372 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now