Skip site navigation (1) Skip section navigation (2)

Re: Cast NULL into Timestamp?

From: Wei Weng <wweng(at)kencast(dot)com>
To: "'pgsql-sql(at)postgresql(dot)org'" <pgsql-sql(at)postgresql(dot)org>
Subject: Cast NULL into Timestamp?
Date: 2004-12-10 19:24:56
Message-ID: 41B9F808.2040909@kencast.com (view raw)
I have a table

create table temp
(
	tempdate	timestamp,
	tempname	varchar(10)
);

And I tried to insert the following:

insert into table temp (tempname, tempdate)
select distinct 'tempname', null from some_other_relevant_table;

And I got an error that says "column "tempdate" is of type timestamp ... but 
expression is of type text... will need to rewrite or cast the expression".

I really need the distinct. Is there anyway to cast this NULL into a 
timestamp or any other workarounds?


Thanks

Wei

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: Wei Weng <wweng(at)kencast(dot)com>,"'pgsql-sql(at)postgresql(dot)org'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Cast NULL into Timestamp?
Date: 2004-12-11 00:10:16
Message-ID: 200412101610.16959.scrawford@pinpointresearch.com (view raw)
On Friday 10 December 2004 11:24 am, Wei Weng wrote:
> I have a table
>
> create table temp
> (
> 	tempdate	timestamp,
> 	tempname	varchar(10)
> );
>
> And I tried to insert the following:
>
> insert into table temp (tempname, tempdate)
> select distinct 'tempname', null from some_other_relevant_table;
>
> And I got an error that says "column "tempdate" is of type
> timestamp ... but expression is of type text... will need to
> rewrite or cast the expression".
>
> I really need the distinct. Is there anyway to cast this NULL into
> a timestamp or any other workarounds?

How about:
insert into table temp (tempname)
select distinct 'tempname' from some_other_relevant_table;

Unless there's something you have left out in describing your setup 
this will leave the tempdate column null.

Cheers,
Steve


From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Wei Weng <wweng(at)kencast(dot)com>
Cc: "'pgsql-sql(at)postgresql(dot)org'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Cast NULL into Timestamp?
Date: 2004-12-11 00:48:24
Message-ID: 20041210164721.A87803@megazone.bigpanda.com (view raw)
On Fri, 10 Dec 2004, Wei Weng wrote:

> I have a table
>
> create table temp
> (
> 	tempdate	timestamp,
> 	tempname	varchar(10)
> );
>
> And I tried to insert the following:
>
> insert into table temp (tempname, tempdate)
> select distinct 'tempname', null from some_other_relevant_table;
>
> And I got an error that says "column "tempdate" is of type timestamp ... but
> expression is of type text... will need to rewrite or cast the expression".
>
> I really need the distinct. Is there anyway to cast this NULL into a
> timestamp or any other workarounds?

CAST(NULL as timestamp) should work.

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: Wei Weng <wweng(at)kencast(dot)com>
Subject: Re: Cast NULL into Timestamp?
Date: 2004-12-11 05:11:28
Message-ID: 200412102111.28104.josh@agliodbs.com (view raw)
Wei,

> insert into table temp (tempname, tempdate)
> select distinct 'tempname', null from some_other_relevant_table;

I don't think you're reporting the error exactly as it happened.  Try cutting 
and pasting your actual PSQL session into your e-mail.

Perhaps you are mixing up the column order?

-- 
Josh Berkus
Aglio Database Solutions
San Francisco

From: Frank Bax <fbax(at)sympatico(dot)ca>
To: pgsql-sql(at)postgresql(dot)org
Cc: Wei Weng <wweng(at)kencast(dot)com>
Subject: Re: Cast NULL into Timestamp?
Date: 2004-12-11 17:10:02
Message-ID: 5.2.1.1.0.20041211115940.02eca9a0@pop6.sympatico.ca (view raw)
At 12:11 AM 12/11/04, Josh Berkus wrote:

>Wei,
>
> > insert into table temp (tempname, tempdate)
> > select distinct 'tempname', null from some_other_relevant_table;
>
>I don't think you're reporting the error exactly as it happened.  Try cutting
>and pasting your actual PSQL session into your e-mail.
>
>Perhaps you are mixing up the column order?


A copy/paste certainly would have been helpful in this case.  There are 
several problems with the above query.
1) the keyword 'table' is not part of insert command.
2) If 'tempname' is a fieldname, it should not have single quotes.
3) NULL doesn't seem to work as expression in select.

If (3) is the real problem here, then either solution proposed by 
Steve/Stephan will work. 



Privacy Policy | About PostgreSQL
Copyright © 1996-2013 The PostgreSQL Global Development Group