【问题标题】:ERROR: function to_timestamp(character varying) does not exist错误:函数 to_timestamp(字符变化)不存在
【发布时间】:2022-02-21 19:38:23
【问题描述】:

我有时间列(字符串),如下所示:

time
1645437571.1399999
1645437571.14
1645437571.1455667
1645437571.2
1645437572
1645738427
1645738427

当我这样做时:

select to_timestamp(1645437571.1399999);

对于该列中的每个值,我总是会得到正确的响应,例如:

2022-02-21 09:59:31.140000 +00:00

但是当我这样做时:

select to_timestamp(time) from pmu;

我得到错误:

[42883] 错误:函数 to_timestamp(字符变化)不存在 提示:没有函数匹配给定的名称和参数类型。你可能 需要添加显式类型转换。位置:8

我知道这是因为字符是不同的,所以如果我定义这样的格式:

select to_timestamp(time, 'DD-MM-YYYY SS:MS:US') from pmu;

比我得到的:

[22008] 错误:日期/时间字段值超出范围:“1645437571.14” 它也应该适用于空值。

【问题讨论】:

  • 最好的解决方案是ALTER 表并将该列更改为timestamp,而不是伪装成字符串的可怕的unix epoch。

标签: sql postgresql datetime timestamp


【解决方案1】:

to_timestamp(varchar) 不存在。你必须在使用 to_timestamp 之前转换你的数据

select to_timestamp(time::numeric) from pmu;

【讨论】:

  • 如果我有空值我应该使用什么,我已经编辑了我的问题,因为我忘了添加它作为我的情况。
  • @Hrvoje:你对空值的定义是什么? NULL 或长度为 0 的 varchar ?
  • 抱歉,我的意思是 NULL - 字段中没有任何内容。
  • 如果值为 null to_timestamp 返回 null Result here
猜你喜欢
  • 2012-05-11
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2018-01-03
  • 2011-07-03
  • 1970-01-01
  • 2014-06-30
  • 2012-05-19
相关资源
最近更新 更多