Solved

T-SQL compounded Case statement

Posted on 2008-10-15
7
218 Views
Last Modified: 2012-05-05
Hi,

With the case staement, I can substitute the value coming from the field like this

Case Field1 when 'Hello' then 'Happy'

I want to decide on the output of Field1 based not just what its value is but also based on a second field.

How can I do this?

Thanks.
0
Comment
Question by:sublimation
  • 3
  • 2
  • 2
7 Comments
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
case when field1 = 1 and field2 = 2 then 3
         when field1=2 and field2 = 3 then 4
         else 5 end
0
 
LVL 4

Author Comment

by:sublimation
Comment Utility
Hi, Momi,

That doesn't work.

(Incorrect syntax near the keyword 'and'.)
0
 
LVL 1

Expert Comment

by:Kumawat_Shobha
Comment Utility
case when field1 = 'hello' and field2 = 'hi'
         then 'happy'
         when field1 != 'hello' and field2 = 'bye'
         then 'sad'
end
0
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.

 
LVL 4

Author Comment

by:sublimation
Comment Utility
Hi,  I want to do this:
 Select case FIELD1
   when ('System' and FIELD2 = 'yes') then 'good system'  -- write value to FIELD1 not FIELD2
   when ('System' and FIELD2 = 'No') then 'Bad System'   -- write value to FIELD1 not FIELD2
else FIELD1 end,
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 250 total points
Comment Utility
Select case   when (FIELD1='System' and FIELD2 = 'yes') then 'good system'  -- write value to FIELD1 not FIELD2
   when (FIELD1='System' and FIELD2 = 'No') then 'Bad System'   -- write value to FIELD1 not FIELD2
else FIELD1 end
0
 
LVL 1

Expert Comment

by:Kumawat_Shobha
Comment Utility
momi's code will work definitely
0
 
LVL 4

Author Closing Comment

by:sublimation
Comment Utility
Thanks.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Viewers will learn how the fundamental information of how to create a table.

763 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

9 Experts available now in Live!

Get 1:1 Help Now