Solved

Nested case statement

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

910 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

22 Experts available now in Live!

Get 1:1 Help Now