如何在 PostgreSQL 中截断日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18695350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to truncate date in PostgreSQL?
提问by yegor256
I'm trying to select all transactions in PostgreSQL 9 that happened earlier than the end of the last week. Which date function I should use to build such an interval?
我正在尝试选择 PostgreSQL 9 中发生在上周末之前的所有事务。我应该使用哪个日期函数来构建这样的间隔?
回答by regilero
> select now();
"2013-09-09 11:43:29.307089+02"
> select date_trunc('week',now()-'1 week'::interval);
"2013-09-02 00:00:00+02" //start of previous week
> select date_trunc('week',now())
"2013-09-09 00:00:00+02" // start of current week
> select date_trunc('week',now())-'1 s'::interval;
"2013-09-08 23:59:59+02" // end of previous week
So using date_trunc('week',now())-'1 s'::interval;
on the right side of your date operator should work. This is a timestamp with time zone value which refers in fact to 23:59:59 on sunday, but with 2 hours of difference with UTC time, depends on your locale and settings.
因此date_trunc('week',now())-'1 s'::interval;
,在日期运算符的右侧使用应该可以工作。这是一个带有时区值的时间戳,实际上是指星期日的 23:59:59,但与 UTC 时间有 2 小时的差异,这取决于您的语言环境和设置。
回答by Joe Taras
You can fix a date or remove days from current day.
您可以修复日期或从当前日期中删除天数。
Show this: are the date/time operators and functions present in Postgres
显示这一点:Postgres 中是否存在日期/时间运算符和函数
I linked 9.1 functions because you've tagged with postgres 9.1, exists too the 9.2 reference page
我链接了 9.1 函数,因为你已经用 postgres 9.1 标记,也存在 9.2 参考页面