'Column is of type timestamp without time zone but expression is of type interval' Redshift

£可爱£侵袭症+ 提交于 2021-01-29 18:07:13

问题


I have one column of timezone names. I want to get another column with the time difference compared to UTC time. But I don't know which data type I should use for this offset column when creating the table. I used:

CREATE TABLE zone_offset(
zone_name varchar(50),
zone_utc_diff timestamp
);

INSERT INTO zone_offset

SELECT zone_name, getdate() - getdate() at time zone zone_name AS zone_utc_diff
FROM zones

and I got that error. How can I make it work with whatever datatype?

Thank you very much in advance!


回答1:


You should probably store zone_utc_diff as an interval, not as a timestamp. Timestamps represent date/times, while intervals represent differences between dates.

Demo on DB Fiddle:

create table zones (
    zone_name varchar(50)  
);    

insert into zones values('cet');

create table zone_offset(
    zone_name varchar(50),
    zone_utc_diff interval
);

insert into zone_offset
select zone_name, current_timestamp - (current_timestamp at time zone zone_name) as zone_utc_diff
from zones


select * from zone_offset;

| zone_name | zone_utc_diff   |
| --------- | --------------- |
| cet       | {"hours":-1}    |


来源:https://stackoverflow.com/questions/58490265/column-is-of-type-timestamp-without-time-zone-but-expression-is-of-type-interva

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!