473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Nested Case Statements

I have a problem, and would like you input...

I need to evaluate to columns and then base a third column on their
values. But, I need to include NULL values and this statement isn't
working, what am I doing wrong? Is this possible?

Example:

CASE A1
WHEN NULL THEN CASE A2
WHEN NULL THEN 'B1'
ELSE 'B2'
END
ELSE 'B3'
END AS C1
So based on if A1 is NULL or not, continue to the nested CASE
statement. If A1 is NOT NULL then assign C1 = B3, but if A1 is NULL AND
A2 is NULL then assign C1 = B1, else assign C1 = B2.

Understand?? Confused??

Any help would be greatly appreciated.
Thanks,
Chris

Dec 14 '05 #1
5 13571

cb******@gmail.com wrote:
I have a problem, and would like you input...

I need to evaluate to columns and then base a third column on their
values. But, I need to include NULL values and this statement isn't
working, what am I doing wrong? Is this possible?

Example:

CASE A1
WHEN NULL THEN CASE A2
WHEN NULL THEN 'B1'
ELSE 'B2'
END
ELSE 'B3'
END AS C1

I would suggest the other possible case construction:

CASE WHEN A1 IS NULL
THEN CASE WHEN A2 IS NULL
THEN 'B1'
ELSE 'B2'
END
ELSE 'B3'
END AS C1

HTH
/Lennart


So based on if A1 is NULL or not, continue to the nested CASE
statement. If A1 is NOT NULL then assign C1 = B3, but if A1 is NULL AND
A2 is NULL then assign C1 = B1, else assign C1 = B2.

Understand?? Confused??

Any help would be greatly appreciated.
Thanks,
Chris


Dec 14 '05 #2
Not an answer, just another way of doing it: COALESCE(A1 || 'B3', A2 ||
'B2', 'B3')

I wonder which is faster.

B.

Dec 14 '05 #3
Thanks Lennart.... works beautifully!

+CB

Dec 14 '05 #4
Brian Tkatch wrote:
Not an answer, just another way of doing it: COALESCE(A1 || 'B3', A2 ||
'B2', 'B3')


This won't work as || is the concatenation operator, and if A1 is indeed
NULL, the concatenation will be NULL and the other parameters of COALESCE
are considered. But if A1 is not NULL, the result is the concatenation of
A1 with 'B3', and that's different from just 'B3'. So you have to resort
to the CASE expressions.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 15 '05 #5
Heh. I was concentrating on it returning NULL to the COALESCE, i was
treating the not-NULL value as a zero-length string. Thanx for the
catch.

Hmm.. shouldn't wrapping that in RIGHT() should take care of that:

CHAR(RIGHT(COALESCE(A1 || 'B3', A2 || 'B2', 'B3'), 2), 2)

What i'm really curious about is if there is a preference to CASE or a
built-in FUNCTION when it comes to performance.

B.

Dec 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
0
by: java7man | last post by:
All - below is part of a construct I am using. I have some very complex data and processing to perfrom and I need a way to get beyond case limitations. Anyone know of other more robust constructs...
1
by: redpayne | last post by:
Ok-I am doing homework out of a book and the instructions are to display an interface with 5 option buttons in a frame. When clicked, each button changes the background color of the frame. It...
6
by: Fir5tSight | last post by:
Hi All, I have a "SELECT" statement in a stored procedure that looks like the follows: ...
9
by: paulyche | last post by:
I'm writing a program which contains an awful lot of nested if statements. I don't know how efficient this is but it definetely makes the code less readable. Does anyone have any advice on how I can...
5
patjones
by: patjones | last post by:
Hi: I have a table in my Access database, "tblWC", which contains a field called "fldTrackingStatus". Furthermore, I have a report "rptTrackingReport" which is based on tblWC (using an SQL query...
1
by: RAJ | last post by:
hi there, can anybody suggest me best sites for learning php with nested sql statements. i need sql query for searching from database with lots of options like using AND, OR etc even using...
0
by: Neil Cerutti | last post by:
The docs say: A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on...
2
lifeisgreat20009
by: lifeisgreat20009 | last post by:
the simple script below is not working after i implemented a case statement within another..plz tell me where the problem lies... My script is as follows #!/bin/bash # A menu driven Shell...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.