Re: [GENERAL] Typecasting datetype as date. How do I cope with NULLs? - Mailing list pgsql-general

From Wim Ceulemans
Subject Re: [GENERAL] Typecasting datetype as date. How do I cope with NULLs?
Date
Msg-id 000701be19e3$2b606200$67faa8c0@aladdin.nice.be
Whole thread Raw
In response to [GENERAL] Typecasting datetype as date. How do I cope with NULLs?  (Stuart Rison <stuart@ludwig.ucl.ac.uk>)
List pgsql-general
>There are, on my mind, at least two answers. For experience I use
>small table 'create table a( dt datetime, i int)'. Hera are data in
>this table (one row has NULL as dt value):
>
>tolik=> select * from a;
>dt                          | i
>----------------------------+--
>Thu Nov 26 16:35:23 1998 MSK| 1
>Wed Nov 25 00:00:00 1998 MSK| 2
>Fri Nov 27 00:00:00 1998 MSK| 3
>                            |10
>
>First use 'union':
>-----------------------------------------------
>select dt::date, i from a where dt is not null
>union
>select NULL,     i from a where dt is null;
>      date| i
>----------+--
>11-25-1998| 2
>11-26-1998| 1
>11-27-1998| 3
>          |10
>(4 rows)
>-----------------------------------------------
>
>
>Second, try use date_trunc('day', dt) instead date_part:
>--------------------------------------------------------------
>tolik=> select date_trunc('day', dt), i from a;
>date_trunc                  | i
>----------------------------+--
>Thu Nov 26 00:00:00 1998 MSK| 1
>Wed Nov 25 00:00:00 1998 MSK| 2
>Fri Nov 27 00:00:00 1998 MSK| 3
>                            |10
>(4 rows)
>
>--------------------------------------------------------------
>
I find the above interesting and I've tried something similar that won't
work.
I have two tables defined as follows

create table test1 (id1 int,link1 int);
create table test2 (id2 int, field2 varchar(5));

where link1 is a foreign key of test1 that should be linked to id2 of test2

Now when I execute the following query:

select id1,field2,link1 from test1,test2 where test1.link1=test2.id2
union
select id1,NULL,link1 from test1;

I always get the following error:

Each UNION query must have identical target types.

Why this error, and what does it mean?

TIA
Wim Ceulemans - wim.ceulemans@nice.be
Nice Software Solutions
Eglegemweg 3, 2811 Hombeek - Belgium
Tel +32(0)15 41 29 53 - Fax +32(0)15 41 29 54





pgsql-general by date:

Previous
From: tolik@icomm.ru (Anatoly K. Lasareff)
Date:
Subject: Re: [GENERAL] Typecasting datetype as date. How do I cope with NULLs?
Next
From: tolik@icomm.ru (Anatoly K. Lasareff)
Date:
Subject: Re: [GENERAL] Typecasting datetype as date. How do I cope with NULLs?