Thread: Cast NULL into Timestamp?

Cast NULL into Timestamp?

From
Wei Weng
Date:
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


Re: Cast NULL into Timestamp?

From
Josh Berkus
Date:
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


Re: Cast NULL into Timestamp?

From
Stephan Szabo
Date:
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.


Re: Cast NULL into Timestamp?

From
Steve Crawford
Date:
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



Re: Cast NULL into Timestamp?

From
Frank Bax
Date:
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.