Solved

T-SQL compounded Case statement

Posted on 2008-10-15
7
219 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
ID: 22719033
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
ID: 22719113
Hi, Momi,

That doesn't work.

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

Expert Comment

by:Kumawat_Shobha
ID: 22719121
case when field1 = 'hello' and field2 = 'hi'
         then 'happy'
         when field1 != 'hello' and field2 = 'bye'
         then 'sad'
end
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 4

Author Comment

by:sublimation
ID: 22719170
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
ID: 22719187
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
ID: 22719286
momi's code will work definitely
0
 
LVL 4

Author Closing Comment

by:sublimation
ID: 31506230
Thanks.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now