问题
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