473,788 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13600

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(COAL ESCE(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
4319
by: Jim Irvine | last post by:
Does anybody know the limit of nested iif statements you can use in Access?
0
3217
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 available in SQL - DB2. The book says only 3 levels of nested case statements are allowed - that is true. Any assistance would be appreciated. select field1 , field2 ,case
1
2455
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 proceeds to tell me to construct CheckboxGroup, use FlowLayaout and add the Checkboxes to the frame along with ItemListener. It says addWindowListener()method, write code for itemStateChanged() which uses the getState() method and nested if statements....
6
1807
by: Fir5tSight | last post by:
Hi All, I have a "SELECT" statement in a stored procedure that looks like the follows: -------------------------------------------------------------------------------------------------------------- SELECT CASE WHEN ( ri.Status NOT LIKE '%COMPLETE%' ) -- report not completed
9
9329
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 improve the following kind of code? Apparently other languages have a switch statement which is useful here...Python seems to lack this functionality. My Python is self-taught so I'd appreciate any outside input. Lots of the code involves...
5
1593
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 written out in VB). The report sorts all the records out by fldTrackingStatus, which can have an integer value between 1 and 5 inclusive. The problem is, the report is putting "1", "2", "3", etc. at the top of each tracking status sub-section. I...
1
1390
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 multiple tables thanks, raj
0
1293
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 subsequent lines. Only the latter form of suite can contain nested compound statements; the following is illegal, mostly because it wouldn't be clear to which if clause a following else clause would belong: if test1: if test2: print x
2
7810
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 script which has following options # Contents of /etc/passwd # List of users currently logged # Simple Calculator
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10112
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.