473,472 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

unexpected result from COALESCE

Any ideas on why my the result 'green' row
column DELTA does not yield -1 for?
That is not how I expected COALESCE( ... ) to work.

Every other row for DELTA (including 'black')
comes out the way I expected.

Thanks for considering this,
John G.

note: running as400, on V5-R2
create table blarch (
a char(10),
b char(10)
);
insert into blarch values( 'red', '123' );
insert into blarch values( 'blue', '0' );
insert into blarch values( 'green', '' );
insert into blarch values( 'black', null );
select
'>>' || a || '<<' as ALPHA ,
'>>' || b || '<<' as BETA ,
cast( b as INTEGER) as GAMMA,
coalesce( cast( b as INTEGER), -1 ) as DELTA
from blarch;

test
| ALPHA | BETA | GAMMA | DELTA |
+--------------+--------------+-----------+-----------+
|>>red <<|>>123 <<| 123| 123|
|>>blue <<|>>0 <<| 0| 0|
|>>green <<|>> <<| -| -|
|>>black <<|- | -| -1|
+--------------+--------------+-----------+-----------+
Nov 12 '05 #1
5 5978
John Greve wrote:
test
| ALPHA | BETA | GAMMA | DELTA |
+--------------+--------------+-----------+-----------+
|>>red <<|>>123 <<| 123| 123|
|>>blue <<|>>0 <<| 0| 0|
|>>green <<|>> <<| -| -|
|>>black <<|- | -| -1|
+--------------+--------------+-----------+-----------+


Here is what I get on DB2 for LUW.
Do you have soem form of friendly-arithmetic goin on that
recovers from the error and fails the whole expression (including the
COALESCE) to NULL maybe?

ALPHA BETA GAMMA DELTA
-------------- -------------- ----------- -----------
red << >>123 << 123 123
blue << >>0 << 0 0

SQL0420N Invalid character found in a character string argument of the
function "INTEGER". SQLSTATE=22018
Nov 12 '05 #2
Thanks, Serge - yes, it does seem to be "friendly" arithmetic at first.
Casting an all-space CHAR value to INTEGER generates a NULL
on DB/2-400. What I am stuck on is why doesn't COALESCE notice
the NULL and, uh... coalesce? Column GAMMA yields NULL for
rows "green" and "black". Why then isn't column DELTA
the same in those rows? You'll recall that DELTA is defined
in the query as:
coalesce( cast( b as INTEGER), -1) as DELTA
I expected -1 to be next in line for consideration by COALESCE
once it found a null resulting from the CAST.

It seems that NULL values are typed in this corner
of the DB/2 multiverse. That is something I have not
seen before in relational-land. Is there any possible
benefit to having different "flavors" of NULL ?

Serge Rielau <sr*****@ca.eye-bee-em.com> wrote in message news:<nX*******************@twister01.bloor.is.net .cable.rogers.com>...
John Greve wrote:
test
| ALPHA | BETA | GAMMA | DELTA |
+--------------+--------------+-----------+-----------+
|>>red <<|>>123 <<| 123| 123|
|>>blue <<|>>0 <<| 0| 0|
|>>green <<|>> <<| -| -|
|>>black <<|- | -| -1|
+--------------+--------------+-----------+-----------+


Here is what I get on DB2 for LUW.
Do you have soem form of friendly-arithmetic goin on that
recovers from the error and fails the whole expression (including the
COALESCE) to NULL maybe?

ALPHA BETA GAMMA DELTA
-------------- -------------- ----------- -----------
>>red << >>123 << 123 123
>>blue << >>0 << 0 0

SQL0420N Invalid character found in a character string argument of the
function "INTEGER". SQLSTATE=22018

Nov 12 '05 #3
AK
can you first COALESCE, then CAST?
Nov 12 '05 #4
John I can only gues that DB2 for iSeries not only NULLs the failing
function, but the whole expression. So the COALESCE (allegedly) never
executes. Pure guesswork on my part....

Cheers
Serge
Nov 12 '05 #5
ak************@yahoo.com (AK) wrote in message news:<46**************************@posting.google. com>...
can you first COALESCE, then CAST?


Start by using COALESCE on the value raw value from disk?
Hmm... for the problem row, the column
in question is actually not null - it
is space filled.

COALESCE would see that, and say "Not null, ok - keep it."
Then CAST would say "Hmm... space, I'll convert that to null."

Since first posting this, I have come up with a "workaround".
cast( '0' || b as INTEGER)

To be null proof, it would be best to use something like this:
cast( '0' || COALESCE(b, '') as INTEGER )

It seems that prefixing the characters with an extra zero
is fairly harmless; fortunately negative numbers aren't
an issue in this application. I doubt cast() would be
very happy with '0-1234'.

Why the database creator put numbers in a CHAR column I'll never know.
Oh well, thanks Serge and AK for pondering this.
Nov 12 '05 #6

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

Similar topics

1
by: per | last post by:
im not very good at sql but need to query the database to use in my programming script. if the database is just like this id name parent_id 1 A ...
2
by: Jeff Roughgarden | last post by:
I am using a clever product called SQL Expert Pro to optimize problematic SQL statements. It works by generating syntactically identical variations of the original SQL, finding all unique execution...
5
by: Krisnamourt Correia via SQLMonster.com | last post by:
I have one query that executes many times in a week. I created one Maintenances plan that Rebuild all index in my Database that has been executed at 23:40 Saturday until stop finished at Sunday. ...
5
by: Bob Stearns | last post by:
I have a table t1(id, other stuff) with 4 dependent (unrelated) tables ta(id, date, other stuff a), tb(id, date, other stuff b), tc(id, date, other stuff c), td(id, date, other stuff d). Any or all...
1
by: Bob Stearns | last post by:
Please explain tome why the third row of my result is not the fourth row. In most Western alphabets 'dd'>'c'. SELECT t1.*, lot_numb, lot_suffix, part_of_lot_numb, part_of_lot_suffix, CASE...
4
by: Don | last post by:
If I have an SQL query which returns an aggregate of several decimal fields like so: (sum(COALESCE(myDecimal1, 0)+ sum(COALESCE(myDecimal2, 0)+ sum(COALESCE(myDecimal3, 0)) as MyTotal I get...
10
by: db2group88 | last post by:
hi, we are using db2 v8.2 EE on windows, with jcc driver. try to create a join view, after that i check the syscat.columns table, for those decimal field use COALESCE method, all the length become...
13
by: bintom | last post by:
I ran the following simple code in C++ and got unexpected results: float f = 139.4; cout << f; Output: 139.399994;
1
by: r035198x | last post by:
This is a short tip describing the sql coalesce function. Description COALESCE accepts a comma separated list of parameters and returns the first value that is not null in that list or null...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.