gpt4 book ai didi

postgresql - postgresql中时间戳的文本

转载 作者:行者123 更新时间:2023-11-29 14:32:34 26 4
gpt4 key购买 nike

在 View 中,我有一个 text 列,其中包含此格式的时间戳 '20/03/2018 00:00' 我正在尝试制作一个使用 between 子句进行选择,但它不起作用

SELECT id,entry_date
FROM v_view
WHERE entrada BETWEEN to_timestamp('20/03/2018 00:00','DD/MM/YYYY')::timestamp and to_timestamp('22/03/2018 00:00')::timestamp
order entry_date

出现这个错误信息

ERROR:  el operador no existe: text >= timestamp without time zone
LINE 3: WHERE entry_date BETWEEN to_timestamp('20/03/2018 00:00','DD/MM.

最佳答案

您需要将 entrada 列值转换为时间戳。

此外:将 to_timestamp() 的结果转换为 timestamp 是无用的,因为 to_timestamp() 已经返回了 timestamp

SELECT id,entry_date
FROM v_view
WHERE to_timestamp(entrada, 'dd/mm/yyyy hh24:mi')
BETWEEN to_timestamp('20/03/2018', 'DD/MM/YYYY')
and to_timestamp('22/03/2018', 'dd/mm/yyyy')
order entry_date;

我更喜欢使用 ANSI SQL 时间戳文字而不是 to_timestamp 函数:

SELECT id,entry_date
FROM v_view
WHERE to_timestamp(entrada, 'dd/mm/yyyy hh24:mi')
BETWEEN timestamp '2018-03-20 00:00:00'
and timestamp '2018-03-22 00:00:00'
order entry_date

不要将日期、时间或时间戳值存储在 textvarchar 列中。您应该将该列定义为 timestamp 然后您不需要转换任何内容,也不需要处理该列中的无效时间戳值。

关于postgresql - postgresql中时间戳的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com