postgresql - 如何将 JSONB 对象中的现有时间戳更改为 Postgresql 中的另一种时间戳格式
问题描述
表user_record
有一个列info
,它返回以下数据,而info
它是一个jsonb
列。
查询: Select info from user_record;
结果:
{"someId": "XXFUN0123XX", "age": 43, "updatedAt": "2017-02-18 00:00:00"}
现在问题是关于updatedAt
json响应
select info ->> 'updatedAt' from user_record where id='XXFUN0123XX';
结果的字段:2017-02-18 00:00:00
我必须使用这种格式更新表列updatedAt
字段的所有时间戳 ->所以当我这样做时,我会得到这样的结果 -> 。
我已经尝试了多种方法并阅读了文档,但找不到合适的解决方案。以下是我试图做的一个查询info
user_record
YYYY-MM-DDTHH:MI:SS+02:00
select info ->> 'updatedAt'
2017-02-18T00:00:00+02:00
UPDATE user_record SET info = info || jsonb_build_object('updatedAt',
select to_timestamp(info ->> 'updatedAt','YYYY-MM-DDTHH:MM:SS+02:00') from user_record where id ='XXFUN0123XX')) where id = 'XXFUN0123XX';
在上面的查询中,我试图updatedAt
从内部查询中提取 json 对象。
解决方案
用于jsonb_set
在 jsonb 中进行更改。并使用to_char
而不是to_timestamp
像下面这样:
update user_record
set info=jsonb_set(info,'{updatedAt}',to_jsonb(to_char((info ->> 'updatedAt')::timestamp,'YYYY-MM-DD"T"HH24:MI:SS+02:00')))
推荐阅读
- r - 在 R 中使用不同的语法拆分列
- c++ - 如何用 boost::future 重新抛出 std::exception_ptr 存储的原始异常?
- angular - 如何在 Angular 4 反应形式 ng-select 中使用管道
- python - 使用 keras 进行多重营销归因
- ruby-on-rails - 在 Rails 中使用 HAML 设置表单样式
- google-cloud-platform - Cloud Foundry cli i/o 超时
- spring-integration - 如何在java中实现集成路由器
- javascript - React + immutable.js:将 Map 合并到 Map 列表中以更新状态
- c++ - 用于验证 Visual Studio 一致性开关对生成代码的影响的工具
- r - 谷歌表格上的 R 脚本