[英]How can we convert a given datetime format in PostgreSQL?
I have a date time column which is of the following format:我有一个日期时间列,格式如下:
2019-11-10-07.10.55.865000 2019-11-10-07.10.55.865000
I want my format to be as follows:我希望我的格式如下:
2019-11-10 07:10:55.865000 2019-11-10 07:10:55.865000
How can I do this in PostgreSQL 9.6.11?如何在 PostgreSQL 9.6.11 中执行此操作?
We can try making a full roundtrip from text to timestamp, then back to text again:我们可以尝试从文本到时间戳进行一次完整的往返,然后再次返回到文本:
SELECT
TO_CHAR(TO_TIMESTAMP('2019-11-10-07.10.55.865000', 'YYYY-MM-DD-HH.MI.SS.US'),
'YYYY-MM-DD HH:MI:SS.US') AS ts_out;
This outputs:这输出:
2019-11-10 07:10:00.865000
As a side note, you should seriously consider not storing your timestamps as text in the first place.作为旁注,您应该认真考虑首先不要将时间戳存储为文本。 Ideally, if you want to view your timestamp column a certain way, eg for reporting purposes, you should only have to make a single call to TO_CHAR
with the format mask you want to use.理想情况下,如果您想以某种方式查看您的时间戳列,例如出于报告目的,您应该只需要使用您想要使用的格式掩码对TO_CHAR
进行一次调用。
There is the to_char(timestamp, text)
function, eg to_char(current_timestamp, 'HH12:MI:SS')
有to_char(timestamp, text)
函数,例如to_char(current_timestamp, 'HH12:MI:SS')
https://www.postgresql.org/docs/current/functions-formatting.html https://www.postgresql.org/docs/current/functions-formatting.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.