Postgres - 将TSTZRANGE拆分为两列

时间:2017-05-05 13:31:34

标签: sql postgresql datetime date-range

我正在使用PostgreSQL 9.4 我在名为timerange的表中有一列,并希望编写一个SELECT查询,该查询将在两个单独的列time_starttime_end中返回时间范围。 我试图像数组一样处理它,但它不起作用:

select *, timerange[0] as t_start from schedules;

当前表:

| id | - - - - - - - - - - -时间范围 - - - - - - - - - - - - - - ----------- |
| ---- | -------------------------------------------- ------------------------------ |
| 1 | [“2017-05-05 19:00:00 + 02”,“2017-05-05 21:00:00 + 02”)|
| 2 | [“2017-05-05 19:00:00 + 02”,“2017-05-05 21:00:00 + 02”)|

所需的表格:

| id | -------- -------------- TIME_START | ------ TIME_END ------------------- |
| ---- | ------------------------------------ | ------- ------------------------------ |
| 1 | “2017-05-05 19:00:00 + 02”| “2017-05-05 21:00:00 + 02”|
| 2 | “2017-05-05 19:00:00 + 02”| “2017-05-05 21:00:00 + 02”|

1 个答案:

答案 0 :(得分:7)

Use lower()upper()

像这样:

SELECT lower(tsrng) AS start, upper(tsrng) AS end
FROM (
  SELECT tstzrange('2017-05-05 12:00:05', '2017-05-05 16:00:05', '[)') AS tsrng
) sub;

或者你的例子:

select *, lower(timerange) as t_start, upper(timerange) as t_end from schedules;