Re: Avoid casting NULLs in UNION...

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Marko Rihtar *EXTERN*" <rihtar(dot)marko(at)gmail(dot)com>, "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Avoid casting NULLs in UNION...
Date: 2013-02-01 10:26:07
Message-ID: A737B7A37273E048B164557ADEF4A58B057AE08C@ntex2010a.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Marko Rihtar wrote:
> maybe you can help me with this issue.
> Here is an example
>
> SELECT NULL::integer
> UNION ALL
> SELECT NULL
> UNION ALL
> SELECT 123
>
> For this sql to work i have to cast NULL to integer.
> I was wondering if there exists some configuration parameter at database level that can be used to
> avoid this need for explicit casting?

Not that I know of.

The behaviour is well defined and documented, see
http://www.postgresql.org/docs/current/static/typeconv-union-case.html

The UNIONs are computed left to right,
and the data type in the first UNION resolves to "text"
according to rule 2 of the page given above.

You can reorder the UNIONs or use parentheses.

Yours,
Laurenz Albe

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Raju Angani 2013-02-01 18:41:15 Zabbix Postbix help
Previous Message Marko Rihtar 2013-02-01 09:43:32 Avoid casting NULLs in UNION...