【问题标题】:Postgres convert string to timePostgres 将字符串转换为时间
【发布时间】:2016-10-14 00:03:14
【问题描述】:

我有以下疑问:

SELECT id, start_date::TIME, occurrence->0->>'startsOn'  FROM service WHERE name='A.F';

返回:

id  | start_date |            ?column?             
------+------------+---------------------------------
 1573 | 18:00:00   | Mon, 29 Jun 2015 18:00:00 +0000
 1592 | 10:00:00   | Wed, 24 Jun 2015 10:00:00 +0000
 1605 | 18:00:00   | Thu, 25 Jun 2015 18:00:00 +0000
 1571 | 10:00:00   | Mon, 29 Jun 2015 10:00:00 +0000
 1591 | 20:15:00   | Tue, 30 Jun 2015 20:15:00 +0000
 1578 | 18:00:00   | Mon, 29 Jun 2015 20:00:00 +0000
 1620 | 12:00:00   | Sat, 27 Jun 2015 12:00:00 +0000
(7 rows)

我要做的是将occurrence->0->>'startsOn' 转换为时间,所以预期的结果应该是:

  id  | start_date |            ?column?             
------+------------+---------------------------------
 1573 | 18:00:00   | 18:00:00
 1592 | 10:00:00   | 10:00:00
 1605 | 18:00:00   | 18:00:00
 1571 | 10:00:00   | 10:00:00
 1591 | 20:15:00   | 20:15:00
 1578 | 18:00:00   | 20:00:00
 1620 | 12:00:00   | 12:00:00

我尝试了以下方法:

SELECT id, start_date::TIME, occurrence->0->>'startsOn'::TIME  FROM service WHERE name='A.F';

But it is not working as it gives me the following syntax error:
ERROR:  invalid input syntax for type time: "startsOn"

【问题讨论】:

  • 括号?..(occurrence->0->>'startsOn')::TIME

标签: sql postgresql type-conversion


【解决方案1】:
select ('[{"startsOn":"Mon, 29 Jun 2015 18:00:00 +0000"}]'::json->0->>'startsOn')::timestamp::time

我没有“发生”列,所以我从您的输出中模拟了它

【讨论】:

    猜你喜欢
    • 2018-10-14
    • 2012-02-03
    • 2020-06-20
    • 2018-07-26
    • 1970-01-01
    • 2011-07-04
    • 2011-10-30
    相关资源
    最近更新 更多