EXTRACT 纪元以语法错误结束 - Postgres

发布于 2025-01-09 02:50:16 字数 1878 浏览 0 评论 0原文

我需要计算两个时间戳的差异(以毫秒为单位)。这是我采取的方法:

-- 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 语句都会失败? 提前致谢。

答案如下: 如何在postgresql中将日期格式转换为毫秒? , 计算日期之间的差异 - Postgres

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 run
select 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 技术交流群。

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

发布评论

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

评论(1

ま昔日黯然 2025-01-16 02:50:16

问题是这样的:

select extract(epoch from timestamp '02/22/2022 7:50'); 
 date_part  
------------
1645516200

select extract(epoch from '02/22/2022 7:50'::timestamp);
 date_part  
------------
 1645516200

select EXTRACT(EPOCH FROM TIMESTAMP now());
ERROR:  syntax error at or near "now"
LINE 1: select EXTRACT(EPOCH FROM TIMESTAMP now());

select timestamp now();
ERROR:  syntax error at or near "("
LINE 1: select timestamp now();

select EXTRACT(EPOCH FROM now());
     date_part     
-------------------
 1645545952.426751

TIMESTAMP 是时间戳字符串到时间戳类型的CAST'02/22/2022 7:50'::timestamp 也完成了同样的事情。无论哪种情况,强制转换都会寻找字符串。您的情况下的 now()_t1/ _t12 已经是时间戳类型,因此您会收到错误。因此,如果您正在使用 timestamp/timestamptz/date 类型的值,请勿使用 TIMESTAMP 对其进行转换。

The issue is this:

select extract(epoch from timestamp '02/22/2022 7:50'); 
 date_part  
------------
1645516200

select extract(epoch from '02/22/2022 7:50'::timestamp);
 date_part  
------------
 1645516200

select EXTRACT(EPOCH FROM TIMESTAMP now());
ERROR:  syntax error at or near "now"
LINE 1: select EXTRACT(EPOCH FROM TIMESTAMP now());

select timestamp now();
ERROR:  syntax error at or near "("
LINE 1: select timestamp now();

select EXTRACT(EPOCH FROM now());
     date_part     
-------------------
 1645545952.426751

The TIMESTAMP is a CAST 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. The now() and _t1/ _t12 in your case are already timestamp types so you get the error. So if you are using a value that is a timestamp/timestamptz/date type already don't use TIMESTAMP to cast it.

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