ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » how to concatenate two strings in esql, if one/both are null

Post new topic  Reply to topic
 how to concatenate two strings in esql, if one/both are null « View previous topic :: View next topic » 
Author Message
geewee
PostPosted: Thu Aug 02, 2012 7:15 am    Post subject: how to concatenate two strings in esql, if one/both are null Reply with quote

Apprentice

Joined: 31 Jul 2012
Posts: 28

I am building up a log message using
SET logmsg = firstPart || secondPart;

this concatenation works as expected if both firstPart and secondPart is not null.

But if either operand is NULL, the result is NULL.

This is unexpected behaviour.

How would you code this to make sure the logmsg actually get as much info as available, and ignoring the null part.
A large if test for every combination of firstPart, secondPart or both null gives just to verbose code for this simple task.
Back to top
View user's profile Send private message
lancelotlinc
PostPosted: Thu Aug 02, 2012 7:23 am    Post subject: Reply with quote

Jedi Knight

Joined: 22 Mar 2010
Posts: 4941
Location: Bloomington, IL USA

That's the behavior as designed:

http://pic.dhe.ibm.com/infocenter/ratdevz/v7r5/index.jsp?topic=%2Fcom.ibm.etools.est.doc%2Fref%2Frsfsql027.html

We use LENGTH to check to see if there is data present.

Code:
IF LENGTH( firstpart ) > 0 and LENGTH( secondPart ) > 0 THEN SET logmsg = firstPart || secondPart;  END IF;

_________________
http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER
Back to top
View user's profile Send private message Send e-mail
mqsiuser
PostPosted: Thu Aug 02, 2012 7:33 am    Post subject: Re: how to concatenate two strings in esql, if one/both are Reply with quote

Yatiri

Joined: 15 Apr 2008
Posts: 637
Location: Germany

try this:
geewee wrote:
SET logmsg = COALESCE(firstPart, '') || COALESCE(secondPart, '');


geewee wrote:
But if either operand is NULL, the result is NULL.

That has to to with computer science... if any operand within a chain of operations is undefined, then the result is undefined. It is like multiplying (somewhere with zero (0)).
Back to top
View user's profile Send private message
mqjeff
PostPosted: Thu Aug 02, 2012 7:39 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

lancelotlinc wrote:
http://pic.dhe.ibm.com/infocenter/ratdevz/v7r5/index.jsp?topic=%2Fcom.ibm.etools.est.doc%2Fref%2Frsfsql027.htm


That link does not point where you think it points, nor does it point anywhere directly helpful.
Back to top
View user's profile Send private message
lancelotlinc
PostPosted: Thu Aug 02, 2012 7:43 am    Post subject: Reply with quote

Jedi Knight

Joined: 22 Mar 2010
Posts: 4941
Location: Bloomington, IL USA

mqjeff wrote:
lancelotlinc wrote:
http://pic.dhe.ibm.com/infocenter/ratdevz/v7r5/index.jsp?topic=%2Fcom.ibm.etools.est.doc%2Fref%2Frsfsql027.htm


That link does not point where you think it points, nor does it point anywhere directly helpful.


Good catch. I may have not copied the right URL.
_________________
http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER
Back to top
View user's profile Send private message Send e-mail
geewee
PostPosted: Thu Aug 02, 2012 8:48 am    Post subject: Reply with quote

Apprentice

Joined: 31 Jul 2012
Posts: 28

Thanks for all replies, and the reference to computer science was very useful, what about mathematics?

Concatenating two strings,

'abb' || null = 'abb'
'abb' || null = null

What makes more sense?
I accept the fact that second option is the chosen way in this area of computer science, I just needed hints on how to code this in efficient way without if-testing for every combination.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Thu Aug 02, 2012 8:54 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

geewee wrote:
Thanks for all replies, and the reference to computer science was very useful, what about mathematics?

Concatenating two strings,

'abb' || null = 'abb'
'abb' || null = null

What makes more sense?


The distinction here is the difference between an element that EXISTS but has NULL as it's value, and an element that DOES NOT exist.

Mathematics only has a notion of an element that has a null value, if I remember my set theory. That is, you can't express
Code:
'abb'||<undefined>


geewee wrote:
I accept the fact that second option is the chosen way in this area of computer science, I just needed hints on how to code this in efficient way without if-testing for every combination.


That's what COALESCE is for.
Back to top
View user's profile Send private message
Vitor
PostPosted: Thu Aug 02, 2012 9:45 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

mqjeff wrote:
geewee wrote:
I accept the fact that second option is the chosen way in this area of computer science, I just needed hints on how to code this in efficient way without if-testing for every combination.


That's what COALESCE is for.




IMHO the COALESCE is less cumbersome than the (perfectly serviceable) LENGTH check. It's my construction of choice.

This in no way is intended to indicate it should be everyone else's choice, or anyone else's choice.
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
geewee
PostPosted: Thu Aug 02, 2012 10:33 am    Post subject: Reply with quote

Apprentice

Joined: 31 Jul 2012
Posts: 28

thanks a lot, mqsiuser

COALESCE is perfect and gives med the expected behaviour in a coding efficient way.

SET logmsg = COALESCE(firstPart, '') || COALESCE(secondPart, '');



regarding the fading discussion on expected behaviour:
A value of null means that the value is unknown, undefined, or uninitialized.

CONCAT(string_expl, string_exp2)
Returns a character string that is the result of concatenating string_exp2 to string_exp1. The resulting string is DBMS dependent. For example, if the column represented by string string_exp1 contains a NULL value, DB2 would return NULL, but SQL Server would return the non-NULL string.

in java, c#:
Why doesn't concatenating with null throw a null reference exception? Is it to make a programmer's life easier, such that they don't have to check for null before concatenation?

In java a null operand is converted to the string literal "null"
Back to top
View user's profile Send private message
mqsiuser
PostPosted: Fri Aug 03, 2012 3:33 am    Post subject: Reply with quote

Yatiri

Joined: 15 Apr 2008
Posts: 637
Location: Germany

I just wanted to point to that this decision that one NULL-value will "whipe out" the other strings (when concatenating) is not like 'a decisons from ESQL', but just the way people probably found out it should be... at least something that people with a CS backround (from some decades ago ) might expect. They argue like: "There is true, there is false, and then there is undefined"... and then this results in logically correct (but to normal people unintuitive) decision tables (for operators that operate on these values).

Probably this is just something coming out of / from the underlying C(++)-code... likely its a decision (to keep it) to make ESQL correct.

We use COALESCE a lot... especially irritating is when your exception-message is nulled out ... so use COALESCE on anything that may be(come) NULL when concatenating strings(CHARs). It is also useful to say e.g. COALESCE( dbValue, 'NULL') on an exception string (instead of saying COALESCE(dbValue, '') all the time).

Quote:
in java, c#:
Why doesn't concatenating with null throw a null reference exception?
Is it to make a programmer's life easier, such that they don't have to check for null before concatenation?


There are NULL pointer exceptions in Java ... but likely yes: (Simple) types in Java need to be initialized with something when they are declared afaik.

Objects use the toString() method. Java has objects only, c# has objects and simple types (which may be boxed to objects if necessary (and then called toString())... there was something called "nullable types" a couple of years ago.

NULL is not the same as 'NULL': Putting the null-string ('null' or 'NULL') into a trace(-file) or an exception-text is likely ok. Returning the null-string (instead of a real/native NULL) during processing can bring you (the developer) into trouble.

Probably (the existens of (it is always there) and to implement or overwrite) "toString()" (in Java/c#) is somewhat equivalent to using COALESCE() (in ESQL).
_________________
Just use REFERENCEs
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » how to concatenate two strings in esql, if one/both are null
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.