gpt4 book ai didi

postgresql - Postgres-必须 to_timestamp() 忽略/不读取日期/时间字符串中间的特定字符

转载 作者:行者123 更新时间:2023-11-29 12:22:19 26 4
gpt4 key购买 nike

我有原始文本列,其值类似于“2012-07-26T10:33:34”和“2012-07-26T10:56:16”。在使用 Joda-Time 的 Java 中,我可以通过调用

轻松地将其转换为日期/从日期转换为日期

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")

在 Postgres 中,我如何忽略中间的“T”字符?

我已经尝试过 'YYYY-MM-DD\THH:mm:ss' 但出现错误

> 错误:找不到从未知到文本的转换函数

最佳答案

如果我没记错的话,T 是允许在 ANSI ANSI 时间戳文字中使用的,所以下面的代码应该可以工作。

select timestamp '2012-07-26T10:33:34';

你说你有一个文本列,所以你可能需要这样的东西:

create table foo (ts text);
insert into foo values ('2012-07-26T10:33:34')
select cast(ts as timestamp)
from foo;

这也适用:

select to_timestamp(ts, 'yyyy-mm-dd hh24:mi:ss')
from foo;

SQLFiddle 示例:http://sqlfiddle.com/#!12/0b369/1

关于postgresql - Postgres-必须 to_timestamp() 忽略/不读取日期/时间字符串中间的特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244185/

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