Postgres - split TSTZRANGE in two columns

末鹿安然 提交于 2020-07-06 13:18:07

问题


I'm using PostgreSQL 9.4 I have column in a table named timerange and want to write a SELECT query which will return time range in two separate columns time_start and time_end. I tried to handle this like an array but it doesn't work:

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

Current table:

| id |                    timerange                        |  
|----|-----------------------------------------------------|  
| 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") |  

Desired table:

| 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:


Use lower() and upper().

Like this:

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;

Or your example:

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


来源:https://stackoverflow.com/questions/43806314/postgres-split-tstzrange-in-two-columns

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