Solved

T-SQL compounded Case statement

Posted on 2008-10-15
7
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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.​
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
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.

756 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