Thread: Is this a feature ?
Hi the list ! I've read the docs, and found that you could concatenate two or more strings in one select statement like this : selectciv || name || forname as identity fromperson; This just works fine and returns rows with only one column named identity. So far, if one of the fields (civ, name or forname) is null, identity is null... That doesn't seems right, since it means that 1+2+0 = 0 !!!! Has anybody else seen the same strange behaviour ? And is there a way to work this around ? Please, don't answer me to use the function textcat (text, text), since it behaves the same and makes queries quite unreadeable... eg. select textcat ( textcat (civ, name), forname) as identity from person; is quite readeable, but when you add fields separator, it isn't. -- David BOURIAUD ---------------------------------------------------------- In a world without walls or fences, what use do we have for windows or gates ? ---------------------------------------------------------- ICQ#102562021
On Tue, 4 Jun 2002, David BOURIAUD wrote: I think is indeed is a feature! Use coalesce. > Hi the list ! > I've read the docs, and found that you could concatenate two or more strings > in one select statement like this : > select > civ || name || forname as identity > from > person; > > This just works fine and returns rows with only one column named identity. > So far, if one of the fields (civ, name or forname) is null, identity is > null... That doesn't seems right, since it means that 1+2+0 = 0 !!!! > Has anybody else seen the same strange behaviour ? And is there a way to work > this around ? > Please, don't answer me to use the function textcat (text, text), since it > behaves the same and makes queries quite unreadeable... > eg. > select > textcat ( textcat (civ, name), forname) as identity > from person; > is quite readeable, but when you add fields separator, it isn't. > -- Achilleus Mantzios S/W Engineer IT dept Dynacom Tankers Mngmt tel: +30-10-8981112 fax: +30-10-8981877 email: achill@matrix.gatewaynet.com mantzios@softlab.ece.ntua.gr
David, yes, this is a feature, and no, we will not change it. You're missing the difference between empty strings or zero value and SQL NULL values. Do you know (hint, hint) how to use the mailing list archives? Jan David BOURIAUD wrote: > Hi the list ! > I've read the docs, and found that you could concatenate two or more strings > in one select statement like this : > select > civ || name || forname as identity > from > person; > > This just works fine and returns rows with only one column named identity. > So far, if one of the fields (civ, name or forname) is null, identity is > null... That doesn't seems right, since it means that 1+2+0 = 0 !!!! > Has anybody else seen the same strange behaviour ? And is there a way to work > this around ? > Please, don't answer me to use the function textcat (text, text), since it > behaves the same and makes queries quite unreadeable... > eg. > select > textcat ( textcat (civ, name), forname) as identity > from person; > is quite readeable, but when you add fields separator, it isn't. > -- > David BOURIAUD > ---------------------------------------------------------- > In a world without walls or fences, what use do we have > for windows or gates ? > ---------------------------------------------------------- > ICQ#102562021 > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster > -- #======================================================================# # It's easier to get forgiveness for being wrong than for being right. # # Let's break this rule - forgive me. # #================================================== JanWieck@Yahoo.com #
> if one of the fields (civ, name or forname) is null, identity is > null... That doesn't seems right, since it means that 1+2+0 = 0 !!!! The crux of the matter is that zero and null aren't the same thing. Null means in essence "We don't know", so this equation is better writtin as: 1+2+"don't know" = "don't know" > is there a > way to work > this around ? It *is* often true that when concatenating strings, we want to tell SQL "if this value is null, treat it like an empty string". This is how you would do it: coalesce( civ,'')||coalesce(name,'')||coalesce(forname,'') Regards, -Nick
Thanks a lot for all the answers you gave me (Nick and the others). I didn't know about this function at all, and now, I'll be abble to write queries that fits my needs. Anyway, I don't use much of these as self queries with psql, since I code mostly with php, which provides concatenation functions that are more usefull for what I need. But now, I know why this is so, even for other SGBDR such as INFORMIX.... > > coalesce( civ,'')||coalesce(name,'')||coalesce(forname,'') > > Regards, > > -Nick > > > > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- David BOURIAUD ---------------------------------------------------------- In a world without walls or fences, what use do we have for windows or gates ? ---------------------------------------------------------- ICQ#102562021
On Tue, 4 Jun 2002, David BOURIAUD wrote: > Hi the list ! > I've read the docs, and found that you could concatenate two or more strings > in one select statement like this : > select > civ || name || forname as identity > from > person; > > This just works fine and returns rows with only one column named identity. > So far, if one of the fields (civ, name or forname) is null, identity is > null... That doesn't seems right, since it means that 1+2+0 = 0 !!!! > Has anybody else seen the same strange behaviour ? And is there a way to work > this around ? Yes, it's a feature, and yes, it's the standard. No, it's not like 1+2+0=0. It's like 1+2+UNKNOWN=UNKNOWN, which is exactly correct. NULL is __not__ the same thing as 0 or empty string; it represents an unknown value. Use the COALESCE function to turn a NULL value into something else. - J. -- Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton Independent Knowledge Management Consultant
On Tuesday 04 June 2002 04:18, David BOURIAUD wrote: > Hi the list ! > I've read the docs, and found that you could concatenate two or more > strings in one select statement like this : > select > civ || name || forname as identity > from > person; > > This just works fine and returns rows with only one column named identity. > So far, if one of the fields (civ, name or forname) is null, identity is > null... That doesn't seems right, since it means that 1+2+0 = 0 !!!! > Has anybody else seen the same strange behaviour ? And is there a way to > work this around ? > Please, don't answer me to use the function textcat (text, text), since it > behaves the same and makes queries quite unreadeable... > eg. > select > textcat ( textcat (civ, name), forname) as identity > from person; > is quite readeable, but when you add fields separator, it isn't. NULL != 0. 1+2+NULL=NULL. Try ... CASE WHEN civ IS NOT NULL THEN civ ELSE '' END || CASE WHEN name IS NOT NULL THEN name ELSE '' END || CASE WHEN forname IS NOT NULL THEN forname ELSE '' END AS identity - James