pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedIs this a feature ?
7+ messages / 6 participants
[nested] [flat]
* Is this a feature ?
@ 2002-06-04 08:18 David BOURIAUD <david.bouriaud@ac-rouen.fr>
0 siblings, 5 replies; 7+ messages in thread
From: David BOURIAUD @ 2002-06-04 08:18 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is this a feature ?
@ 2002-06-04 08:32 Achilleus Mantzios <achill@matrix.gatewaynet.com>
parent: David BOURIAUD <david.bouriaud@ac-rouen.fr>
4 siblings, 0 replies; 7+ messages in thread
From: Achilleus Mantzios @ 2002-06-04 08:32 UTC (permalink / raw)
To: David BOURIAUD <david.bouriaud@ac-rouen.fr>; +Cc: pgsql-sql
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is this a feature ?
@ 2002-06-04 14:33 Jan Wieck <janwieck@yahoo.com>
parent: David BOURIAUD <david.bouriaud@ac-rouen.fr>
4 siblings, 0 replies; 7+ messages in thread
From: Jan Wieck @ 2002-06-04 14:33 UTC (permalink / raw)
To: David BOURIAUD <david.bouriaud@ac-rouen.fr>; +Cc: pgsql-sql
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 #
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is this a feature ?
@ 2002-06-04 14:38 Nick Fankhauser <nickf@ontko.com>
parent: David BOURIAUD <david.bouriaud@ac-rouen.fr>
4 siblings, 1 reply; 7+ messages in thread
From: Nick Fankhauser @ 2002-06-04 14:38 UTC (permalink / raw)
To: David BOURIAUD <david.bouriaud@ac-rouen.fr>; pgsql-sql
> 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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is this a feature ?
@ 2002-06-04 14:55 David BOURIAUD <david.bouriaud@ac-rouen.fr>
parent: Nick Fankhauser <nickf@ontko.com>
0 siblings, 0 replies; 7+ messages in thread
From: David BOURIAUD @ 2002-06-04 14:55 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is this a feature ?
@ 2002-06-04 15:14 James Orr <james@orrwhat.net>
parent: David BOURIAUD <david.bouriaud@ac-rouen.fr>
4 siblings, 0 replies; 7+ messages in thread
From: James Orr @ 2002-06-04 15:14 UTC (permalink / raw)
To: David BOURIAUD <david.bouriaud@ac-rouen.fr>; pgsql-sql
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Is this a feature ?
@ 2002-06-04 17:26 Joel Burton <joel@joelburton.com>
parent: David BOURIAUD <david.bouriaud@ac-rouen.fr>
4 siblings, 0 replies; 7+ messages in thread
From: Joel Burton @ 2002-06-04 17:26 UTC (permalink / raw)
To: David BOURIAUD <david.bouriaud@ac-rouen.fr>; +Cc: pgsql-sql
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
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2002-06-04 17:26 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-06-04 08:18 Is this a feature ? David BOURIAUD <david.bouriaud@ac-rouen.fr>
2002-06-04 08:32 ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
2002-06-04 14:33 ` Jan Wieck <janwieck@yahoo.com>
2002-06-04 14:38 ` Nick Fankhauser <nickf@ontko.com>
2002-06-04 14:55 ` David BOURIAUD <david.bouriaud@ac-rouen.fr>
2002-06-04 15:14 ` James Orr <james@orrwhat.net>
2002-06-04 17:26 ` Joel Burton <joel@joelburton.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox