从Postgres中的`current_date + interval`数据类型中提取日期。

huangapple go评论99阅读模式
英文:

extract only date from current_date + interval data type in postgres

问题

我正在尝试从以下语句中在PostgreSQL中仅获取日期,而不包括时间:

select current_date - date_trunc('day', interval '1 month');

但它返回给我:

2023-02-07 00:00:00

这里可能出了什么问题。我看到date_trunc函数返回时间戳,而间隔无法转换为日期类型:

select current_date - interval '1 month'::date;
英文:

I am trying to get only date without time in postgres from the following statement:

select current_date - date_trunc('day',interval '1 month');

But returns me that:

2023-02-07 00:00:00

What could be going wrong here. I see that date_trunc function returns timestamp and intervals cannot be cast to date type:

select current_date - interval '1 month'::date;

答案1

得分: 1

你可以将interval表达式放入闭包中,然后进行强制转换:

select (current_date - interval '1 month')::date; -- 2023-02-07
英文:

You should be able to place the interval expression into a closure, and then cast that:

<!-- language: sql -->

select (current_date - interval &#39;1 month&#39;)::date;  -- 2023-02-07

huangapple
  • 本文由 发表于 2023年3月7日 10:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定