简体   繁体   中英

RedShift - update table with date from a timestamp column

I have a table named adata in redshift which contains a column timestamp with integer type data variable. The timestamp column holds the timestamp as epoch values. I have added a new column called rdate with date variable type. I am trying to insert date value into rdate column by converting the timestamp column.

What is working;

select timestamp 'epoch' + timestamp * interval '1 second' AS rdate from adata;

This prints the rdate column with timestamp as date and time nicely.

what is not working

insert into adata (rdate) select timestamp 'epoch' + timestamp * interval '1 second' AS rdate from adata;

I tried with an update statement:

update adata set rdate = (select timestamp 'epoch' + timestamp * interval '1 second' AS rdate from adata);


ts              rdate
1611306839      need date here
1611469226      need date here
1611399334      need date here
1611373685      need date here

another option which I tried:

update adata set rdate = (select (timestamp 'epoch' + timestamp * interval '1 second') AS rdate from adata);

It appears that you want to update existing rows in a table . In that case, you would use an UPDATE command.

Since the rows will be updated based upon the value of other columns in the same table, you could simply use:

UPDATE adata SET rdate = timestamp 'epoch' + timestamp * interval '1 second'

where timestamp is the column that contains the existing value.

(Using INSERT would add additional rows, which probably isn't what you are seeking.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM