英文:
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 '1 month')::date; -- 2023-02-07
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论