【发布时间】:2017-10-09 21:52:34
【问题描述】:
我有一个表,其中包含一个 date 列和一个包含时区的 varchar 列。我想编写一个查询,选择时区午夜日期在给定timestamp with time zone 之前的所有行。
例如,给定
CREATE TABLE scheduled_tasks (
id serial primary key,
run_date date not null,
customer_timezone varchar not null
);
INSERT INTO scheduled_tasks(run_date, customer_timezone) VALUES
('2017-10-15', 'America/Los_Angeles'),
('2017-10-15', 'America/New_York'),
('2017-10-15', 'Asia/Tokyo');
如果我希望每个任务在其关联时区的午夜之后运行,我将如何编写查询以查找应在 2017-10-15T06:00:00Z 运行的所有 scheduled_tasks? (所以我想要东京和纽约的任务,而不是洛杉矶的任务,因为2017-10-15T06:00:00Z 是洛杉矶 14 日晚上 11:00。)
我看到了make_timestamptz 函数,但我最好的选择真的是:
SELECT *
FROM scheduled_tasks
WHERE make_timestamptz(
EXTRACT (YEAR from run_date)::int,
EXTRACT (MONTH from run_date)::int,
EXTRACT (DAY from run_date)::int,
0,
0,
0.0,
customer_timezone) < '2017-10-15T06:00:00';
SqlFiddle:http://sqlfiddle.com/#!17/a8e38/4/0
【问题讨论】:
标签: postgresql date datetime