【问题标题】:How do I convert character varying to timestamp in postgresql which is in the format [ "2020-09-21T13:56:58Z" ]如何在 postgresql 中将字符变化转换为时间戳,格式为 [“2020-09-21T13:56:58Z”]
【发布时间】:2021-03-20 15:42:31
【问题描述】:

我尝试这样做,但出现以下错误:

错误:列“2020-09-21t13:56:58z”不存在
Select to_timestamp("2020-09-21T13:56:58Z", "YYYY-MM-DDTHH24:MI:SSZ");

【问题讨论】:

  • 您是否指定了要从中选择的表?
  • 我的栏目名称是“日期”
  • 是的,但你必须指定一个像SELECT to_timestamp(/*date*/) FROM table_1 ;这样的表
  • 从 raw_events 中选择 to_timestamp(date, "YYYY-MM-DDTHH24:MI:SSZ");
  • 如果您使用正确的字符串分隔符,您的代码可以正常工作:Select to_timestamp('2020-09-21T13:56:58Z', 'YYYY-MM-DDTHH24:MI:SSZ')。我投票结束是一个错字。

标签: sql date datetime formatting postgresql-9.2


【解决方案1】:

改用to_char()。如下-

样本数据

create table a (
  date_time timestamp
  );
  
insert into a (date_time) values ('2020-09-21T13:56:58Z')

SELECT

select to_char(date_time, 'YYYY-MM-DDTHHH24:MI:SSZ') as time
from a

结果:

time
2020-09-21ST13:56:58Z

See db<>fiddle

【讨论】:

  • 错误:无效数字,值“-”,位置 4,类型:十进制详细信息:------------------------ ----------------------- 错误:无效数字,值“-”,位置 4,类型:十进制代码:1207 上下文:2020-11-01T01: 04:17Z 查询:541524 位置::0 进程:query0_117_541524 [pid=0] -------------------------------- ---------------
  • @VardhamanJain - 请参阅测试链接中的数据和答案,尝试您在该站点中使用的值
  • @VardhamanJain 使用链接中的示例数据编辑了答案
猜你喜欢
  • 1970-01-01
  • 2020-01-27
  • 2017-03-20
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2019-12-13
  • 1970-01-01
相关资源
最近更新 更多