将 oracle 时间戳转换为 postgres 时间戳?
Conversion of oracle timestamp to postgres timestamp?
oracle 时间戳示例:26-APR-17 09.40.13.243356000
你好,我有一个场景,我正在将 oracle 时间戳转换为 postgres 时间戳,但输出不是预期的。
在 psql
中查询 运行
migration=# SELECT TO_TIMESTAMP('26-APR-17 09.40.13.243356000','DD-MON-YY HH24.MI.SS:MS') :: timestamp without time zone;
to_timestamp
----------------------------
2017-04-26 09:15:55.864128 ----- this is output
(1 row)
输出应该是:2017-04-26 09:40:13.243356
该错误是由于毫秒格式掩码 MS
的无效值造成的。
As documented in the manual 毫秒的允许值是从 000 到 999
然而,.243356000
也对微秒无效(US
格式掩码),因为尾随零。
如果要使用该格式,则必须从输入字符串中删除零,例如:使用 rtrim()
psql (9.6.3)
Type "help" for help.
postgres> SELECT TO_TIMESTAMP(rtrim('26-APR-17 09.40.13.243356000','0'),'DD-MON-YY HH24.MI.SS.US');
to_timestamp
-------------------------------
2017-04-26 09:40:13.243356+02
(1 row)
postgres>
我也同意 PanagiotisKanavos 的观点,最好使用独立于语言环境的格式(即月份数字和 4 位数年份),最好是时间戳值的 ISO 标准。
问题是243356000是按毫秒计算的,这就造成了大约半小时的奇怪偏移。
Trim 最后三个数字并使用 US
表示微秒:
SELECT to_timestamp(
regexp_replace(
'26-APR-17 09.40.13.243356000',
'\d{3}$',
''
),
'DD-MON-YY HH24.MI.SS.US'
);
oracle 时间戳示例:26-APR-17 09.40.13.243356000
你好,我有一个场景,我正在将 oracle 时间戳转换为 postgres 时间戳,但输出不是预期的。
在 psql
中查询 运行migration=# SELECT TO_TIMESTAMP('26-APR-17 09.40.13.243356000','DD-MON-YY HH24.MI.SS:MS') :: timestamp without time zone;
to_timestamp
----------------------------
2017-04-26 09:15:55.864128 ----- this is output
(1 row)
输出应该是:2017-04-26 09:40:13.243356
该错误是由于毫秒格式掩码 MS
的无效值造成的。
As documented in the manual 毫秒的允许值是从 000 到 999
然而,.243356000
也对微秒无效(US
格式掩码),因为尾随零。
如果要使用该格式,则必须从输入字符串中删除零,例如:使用 rtrim()
psql (9.6.3)
Type "help" for help.
postgres> SELECT TO_TIMESTAMP(rtrim('26-APR-17 09.40.13.243356000','0'),'DD-MON-YY HH24.MI.SS.US');
to_timestamp
-------------------------------
2017-04-26 09:40:13.243356+02
(1 row)
postgres>
我也同意 PanagiotisKanavos 的观点,最好使用独立于语言环境的格式(即月份数字和 4 位数年份),最好是时间戳值的 ISO 标准。
问题是243356000是按毫秒计算的,这就造成了大约半小时的奇怪偏移。
Trim 最后三个数字并使用 US
表示微秒:
SELECT to_timestamp(
regexp_replace(
'26-APR-17 09.40.13.243356000',
'\d{3}$',
''
),
'DD-MON-YY HH24.MI.SS.US'
);