如何转换Yyyy-MM-DDTHH:MM:SS.SSSS时间戳为Yyyy-MM-DD HH:MM:Postgres中的SS

发布于 2025-02-07 13:25:55 字数 1131 浏览 1 评论 0原文

问题摘要

我想转换和验证Yyyy-Mm-Ddthh中的时间戳:MM:SS.SSSSZ格式(例如:2022-06-15T08:27:00.5999z)在Postgres中。

我尝试了

select * from my_table WHERE my_date >= date_trunc('second', '2022-06-15T08:27:00.599Z'::timestamp)

select to_char(to_timestamp('2022-06-15T08:27:00.599Z','YYYY-MM-DD\"T\"HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')

这些查询在db_fiddle中罚款,但是在Python和Postgres SQL运行时会出现错误。

面对错误

rror:teiid31100解析错误:遇到 “'2022-06-15T08:27:00.599z','yyyy-mm-dd“ t” t“ hh24:mi:ss), '[] yyyy [] - mm“在第1行,第672列。期望:”和“ |”之间。 | “在” | “是” | “喜欢” | “像_regex” | “不是” | “或” | “订单” | “类似” ... org.teiid.jdbc.teiidsqlexception:teiid31100解析 错误:遇到 “'2022-06-15T08:27:00.599z','yyyy-mm-dd“ t” t“ hh24:mi:ss), '[] yyyy [] - mm“在第1行,第672列。期望:”和“ |”之间。 | “在” | “是” | “喜欢” | “像_regex” | “不是” | “或” | “订单” | “类似” ...;执行查询时错误

我的需要

输入时间戳:2022-06-15 t 08:27:00.599 z
预期时间戳:2022-06-15 08:27:00

感谢您的支持。

Problem Summary

I would like to convert and validate the timestamp which is in YYYY-MM-DDTHH:mm:ss.SSSZ format (ex: 2022-06-15T08:27:00.599Z) in postgres.

I tried

select * from my_table WHERE my_date >= date_trunc('second', '2022-06-15T08:27:00.599Z'::timestamp)

select to_char(to_timestamp('2022-06-15T08:27:00.599Z','YYYY-MM-DD\"T\"HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')

These queries are executing fine in db_fiddle but giving an errors while running in python and postgres SQL.

Facing errors

RROR: TEIID31100 Parsing error: Encountered
"'2022-06-15T08:27:00.599Z','YYYY-MM-DD"T"HH24:MI:SS),
'[]YYYY[]-MM" at line 1, column 672.Was expecting: "and" | "between"
| "in" | "is" | "like" | "like_regex" | "not" | "or" | "order" |
"similar" ...org.teiid.jdbc.TeiidSQLException: TEIID31100 Parsing
error: Encountered
"'2022-06-15T08:27:00.599Z','YYYY-MM-DD"T"HH24:MI:SS),
'[]YYYY[]-MM" at line 1, column 672.Was expecting: "and" | "between"
| "in" | "is" | "like" | "like_regex" | "not" | "or" | "order" |
"similar" ...;Error while executing the query nil

My need

Input timestamp : 2022-06-15T08:27:00.599Z
Expected timestamp : 2022-06-15 08:27:00

Appreciate your support.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2025-02-14 13:25:55

如果我过分简化您的问题,请原谅我,但是简单的演员和date_trunc会做技巧吗?

SELECT date_trunc('second','2022-06-15T08:27:00.599Z'::timestamp);

     date_trunc      
---------------------
 2022-06-15 08:27:00

另一种选择是用:: timestamp(0) - 请参阅此另一个 答案>答案< /代码>。但这将返回不同的结果:

SELECT '2022-06-15T08:27:00.599Z'::timestamp(0);

      timestamp      
---------------------
 2022-06-15 08:27:01

Forgive me if I am oversimplifying your question, but wouldn't a simple cast and date_trunc do the trick?

SELECT date_trunc('second','2022-06-15T08:27:00.599Z'::timestamp);

     date_trunc      
---------------------
 2022-06-15 08:27:00

An alternative is to round the seconds with ::timestamp(0) - see this other answer. But it would return a different result:

SELECT '2022-06-15T08:27:00.599Z'::timestamp(0);

      timestamp      
---------------------
 2022-06-15 08:27:01
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文