EXTRACT 纪元以语法错误结束 - Postgres
我需要计算两个时间戳的差异(以毫秒为单位)。这是我采取的方法:
-- calculate the differenz of two timestamps in milliseconds
DO $$
declare _t1 timestamp;
declare _t2 timestamp;
declare _extract_ms double precision;
BEGIN
_t1 = now();
_t2 = (_t1 - INTERVAL '1 HOUR');
-- _extract_ms = EXTRACT(EPOCH FROM TIMESTAMP _t1::timestamp) - EXTRACT(EPOCH FROM TIMESTAMP _t2::timestamp); -- UNCOMMENT 01
-- _extract_ms = EXTRACT(EPOCH FROM date_trunc('milliseconds', _t1)) - EXTRACT(EPOCH FROM date_trunc('milliseconds', _t2)); -- UNCOMMENT 02
RAISE NOTICE '_t1=% _t2=% _extract_ms=%', _t1, _t2, _extract_ms;
END;
$$ LANGUAGE plpgsql;
0 - 执行前面所示的代码,会产生以下输出:
HINT: _t1=2022-02-22 14:14:59.627456 _t2=2022-02-22 13:14: 59.627456 _extract_ms=
1 - 现在取消注释标有“UNCOMMENT 01”的行并再次执行代码以:
ERROR: Syntax error at »_t1«
LINE 9: _extract_ms = EXTRACT(EPOCH FROM TIMESTAMP _t1::timestamp...
2 - 现在再次注释标有“UNCOMMENT 01”的行并取消注释标有“UNCOMMENT 02”的行并再次执行:
提示:_t1= 2022-02-22 14:28:43.161478 _t2=2022-02-22 13:28:43.161478 _extract_ms=3600
这是预期的结果!
如果我只是跑 选择 EXTRACT(EPOCH FROM TIMESTAMP now());
我发现
ERROR: Syntax error at »now«
LINE 1: select EXTRACT(EPOCH FROM TIMESTAMP now());
我被 pgplsql: SELECT EXTRACT(... ) functioniert nicht 并认为这是解决方案 - 不适合我。
那么,谁能解释一下为什么变体 1 甚至简单的 select 语句都会失败? 提前致谢。
I had the need, to calculate the difference of two timestamp in miliseconds. This is the approach i made:
-- calculate the differenz of two timestamps in milliseconds
DO $
declare _t1 timestamp;
declare _t2 timestamp;
declare _extract_ms double precision;
BEGIN
_t1 = now();
_t2 = (_t1 - INTERVAL '1 HOUR');
-- _extract_ms = EXTRACT(EPOCH FROM TIMESTAMP _t1::timestamp) - EXTRACT(EPOCH FROM TIMESTAMP _t2::timestamp); -- UNCOMMENT 01
-- _extract_ms = EXTRACT(EPOCH FROM date_trunc('milliseconds', _t1)) - EXTRACT(EPOCH FROM date_trunc('milliseconds', _t2)); -- UNCOMMENT 02
RAISE NOTICE '_t1=% _t2=% _extract_ms=%', _t1, _t2, _extract_ms;
END;
$ LANGUAGE plpgsql;
0 - Executing the code as shown before, results in the following output:
HINT: _t1=2022-02-22 14:14:59.627456 _t2=2022-02-22 13:14:59.627456 _extract_ms=<NULL>
1 - Now uncomment the line marked with "UNCOMMENT 01" and execute the code again ends with:
ERROR: Syntax error at »_t1«
LINE 9: _extract_ms = EXTRACT(EPOCH FROM TIMESTAMP _t1::timestamp...
2 - Now comment line marked with "UNCOMMENT 01" again and uncomment line marked with "UNCOMMENT 02" and execute it again:
HINT: _t1=2022-02-22 14:28:43.161478 _t2=2022-02-22 13:28:43.161478 _extract_ms=3600
This is the expected result!
If I simply runselect EXTRACT(EPOCH FROM TIMESTAMP now());
I get
ERROR: Syntax error at »now«
LINE 1: select EXTRACT(EPOCH FROM TIMESTAMP now());
I stumpled over pgplsql: SELECT EXTRACT(...) functioniert nicht and thought this is the solution - not for me.
So, can anyone explain me why variant 1 and even the simply select-statement fails?
Thanks in advance.
Answers read:
How to convert date format into milliseconds in postgresql?,
Calculate difference between dates - Postgres
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是这样的:
TIMESTAMP
是时间戳字符串到时间戳类型的CAST
。'02/22/2022 7:50'::timestamp
也完成了同样的事情。无论哪种情况,强制转换都会寻找字符串。您的情况下的now()
和_t1/ _t12
已经是时间戳类型,因此您会收到错误。因此,如果您正在使用timestamp/timestamptz/date
类型的值,请勿使用TIMESTAMP
对其进行转换。The issue is this:
The
TIMESTAMP
is aCAST
of the timestamp string to a timestamp type. The same thing is done by the'02/22/2022 7:50'::timestamp
. In either case the cast is looking for a string. Thenow()
and_t1/ _t12
in your case are already timestamp types so you get the error. So if you are using a value that is atimestamp/timestamptz/date
type already don't useTIMESTAMP
to cast it.