concatenation operator || with "null" array

Поиск
Список
Период
Сортировка
Искать

concatenation operator || with "null" array

От:
"stroncococcus" <stroncococcus@gmx.de>
Дата:
Hello!

When I try to fill an array with the concatenation operator, like
UPDATE test SET myint = myint || ARRAY[123] WHERE id = 1
that before that statement was null, then it is also null after that
statement.
But if there is already something in that array and I execute that
statement, then everything works fine and one can find the 123 there,
too.
Is this the normal behavior? Is there a way to "concatenate" to null
arrays as well, or do I have to test this inside my script, and if it
is null fill it normal for the first time?

Best regards,
Kai

Re: concatenation operator || with "null" array

От:
"stroncococcus" <stroncococcus@gmx.de>
Дата:
Ok, solved the problem with COALESCE.

Re: concatenation operator || with "null" array

От:
"stroncococcus" <stroncococcus@gmx.de>
Дата:

stroncococcus wrote:
> Ok, solved the problem with COALESCE.

Hm, I not really solved it ... just solved it for text columns, but not
for integer arrays.
I can use this for text
COALESCE(textcol, '') || 'str '
but how do I use this for integers?
Is there a way to create an empty integer array and do such a thing ...
COALESCE(intarraycol, *empty int array*) || 5

Re: concatenation operator || with "null" array

От:
"Brandon Aiken" <BAiken@winemantech.com>
Дата:
NULL concatenated to anything is NULL.  Try this:

UPDATE test SET myint = COALESCE(myint || ARRAY[123], ARRAY[123]) WHERE
id = 1;

Or:

UPDATE test SET myint = 
   CASE WHEN myint IS NULL THEN ARRAY[123]
   ELSE myint || ARRAY[123]
   END
WHERE id = 1;

An empty array can be displayed as ARRAY[NULL], but defaults to type
TEXT.  An explicit empty integer array would be ARRAY[NULL]::INTEGER[].
NULL arrays are not handled entirely consistently, though.  Sometimes it
acts like a NULL, and sometimes it acts like a container of NULL.



--
Brandon Aiken
CS/IT Systems Engineer
-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of stroncococcus
Sent: Wednesday, December 06, 2006 5:43 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] concatenation operator || with "null" array

Hello!

When I try to fill an array with the concatenation operator, like
UPDATE test SET myint = myint || ARRAY[123] WHERE id = 1
that before that statement was null, then it is also null after that
statement.
But if there is already something in that array and I execute that
statement, then everything works fine and one can find the 123 there,
too.
Is this the normal behavior? Is there a way to "concatenate" to null
arrays as well, or do I have to test this inside my script, and if it
is null fill it normal for the first time?

Best regards,
Kai


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

Re: concatenation operator || with "null" array

От:
Bruce Momjian <bruce@momjian.us>
Дата:

The question of concatentation using NULLs comes up enough that I have
added an item to an existing FAQ entry for it, patch attached.

---------------------------------------------------------------------------

stroncococcus wrote:
> Hello!
> 
> When I try to fill an array with the concatenation operator, like
> UPDATE test SET myint = myint || ARRAY[123] WHERE id = 1
> that before that statement was null, then it is also null after that
> statement.
> But if there is already something in that array and I execute that
> statement, then everything works fine and one can find the 123 there,
> too.
> Is this the normal behavior? Is there a way to "concatenate" to null
> arrays as well, or do I have to test this inside my script, and if it
> is null fill it normal for the first time?
> 
> Best regards,
> Kai
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match

-- 
  Bruce Momjian   bruce@momjian.us
  EnterpriseDB    http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
Index: FAQ.html
===================================================================
RCS file: /cvsroot/pgsql/doc/src/FAQ/FAQ.html,v
retrieving revision 1.360
retrieving revision 1.361
diff -c -c -r1.360 -r1.361
*** FAQ.html	5 Dec 2006 23:13:41 -0000	1.360
--- FAQ.html	11 Dec 2006 22:44:53 -0000	1.361
***************
*** 10,16 ****
    alink="#0000ff">
      

Frequently Asked Questions (FAQ) for PostgreSQL

!

Last updated: Tue Dec 5 18:13:32 EST 2006

Current maintainer: Bruce Momjian (bruce@momjian.us) --- 10,16 ---- alink="#0000ff">

Frequently Asked Questions (FAQ) for PostgreSQL

!

Last updated: Mon Dec 11 17:44:33 EST 2006

Current maintainer: Bruce Momjian (bruce@momjian.us) *************** *** 86,93 **** searches and case-insensitive regular expression searches? How do I use an index for case-insensitive searches?
4.9) In a query, how do I detect if a field ! is NULL? How can I sort on whether a field is ! NULL or not?
4.10) What is the difference between the various character types?
4.11.1) How do I create a --- 86,93 ---- searches and case-insensitive regular expression searches? How do I use an index for case-insensitive searches?
4.9) In a query, how do I detect if a field ! is NULL? How do I concatenate possible NULLs? ! How can I sort on whether a field is NULL or not?
4.10) What is the difference between the various character types?
4.11.1) How do I create a *************** *** 823,832 **** identical values that differ only in case. To force a particular case to be stored in the column, use a CHECK constraint or a trigger.

!

4.9) In a query, how do I detect if a field ! is NULL? How can I sort on whether a field is ! NULL or not?

You test the column with IS NULL and IS NOT NULL, like this:

--- 823,836 ---- identical values that differ only in case. To force a particular case to be stored in the column, use a CHECK constraint or a trigger.

! ! 4.9) In a query, how do I detect if a field ! is NULL? How do I concatenate possible NULLs? ! How can I sort on whether a field is NULL or not?
!

4.9) In a query, how do I detect if a field ! is NULL? How do I concatenate possible NULLs? ! How can I sort on whether a field is NULL or not?

You test the column with IS NULL and IS NOT NULL, like this:

*************** *** 837,842 **** --- 841,853 ---- WHERE col IS NULL; +

To concatentate with possible NULLs, use COALESCE(), + like this:

+
+    SELECT COALESCE(col1, '') || COALESCE(col2, '')
+    FROM tab
+ 
+

To sort by the NULL status, use the IS NULL and IS NOT NULL modifiers in your ORDER BY clause. Things that are true will sort higher than things that are false,

FAQ