Solved

T-SQL compounded Case statement

Posted on 2008-10-15
7
220 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
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.

 
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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

776 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