Solved

T-SQL compounded Case statement

Posted on 2008-10-15
7
221 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check ALL SP in database make sure there are no errors 17 43
CDC and AOG on MS SQL 2012 13 23
TSQL convert date to string 4 34
MS SQL SERVER and ADODB.commands 8 14
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

791 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