sql - Postgres - 将 TSTZRANGE 分成两列

标签 sql postgresql datetime date-range

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

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

当前表:

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

所需的表:

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

最佳答案

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;

关于sql - Postgres - 将 TSTZRANGE 分成两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806314/

相关文章:

SQL递归+列连接

mysql - 带 case + union + like 的 where 子句中的未知列

python - 错误 : Failed building wheel for psycopg2

sql - Postgres : Inserting rows into table with foreign key

java - 将当前 UTC 时间的日期时间插入数据库

mysql - 如何修复 MySQL 8 错误代码 : 1525 and 1292?

sql - 如何从 table1 中选择前 500 行并将它们插入到 table2?

php - SQL 显示与客户端 ID 匹配的所有结果,但仅显示每个实例的最新版本

mysql - Sequelize.js 模型查询对于 mySQL 和 PostgreSQL 是否相同?

java - 在Java中解析日期时间格式并将其从csv文件转换为mysql