postgresql 当年的第一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11080411/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:00:42  来源:igfitidea点击:

First day of current year

sqlpostgresql

提问by user1392203

I have the below query in a postgresql database

我在 postgresql 数据库中有以下查询

SELECT * 
FROM accounts 
where insertdate BETWEEN '2012-01-01' AND CURRENT_TIMESTAMP

So how can I replace the '2012-01-01'asking for the first day of the current year

那么我怎样才能取代'2012-01-01'今年第一天的要求

There is one more issue. When I m having a new record in the account table the same moment is running the above select so it doesnt bring me the record I have just made.Is it reasonable? What is the best way to overtake it?

还有一个问题。当我在帐户表中有一条新记录时,同时运行上述选择,所以它不会给我带来我刚刚创建的记录。这合理吗?超越它的最佳方法是什么?

回答by beerbajay

You're looking for date_trunc(), which can truncate a date to a specified precision (e.g. year, month, day):

您正在寻找date_trunc(),它可以将日期截断到指定的精度(例如year, month, day):

SELECT date_trunc('year', now());

In your query:

在您的查询中:

SELECT * FROM accounts where insertdate BETWEEN 
date_trunc('year', now()) AND CURRENT_TIMESTAMP

回答by Rish

You can try and use this CURRENT_TIMESTAMP.YEAR-01-01

您可以尝试使用此 CURRENT_TIMESTAMP.YEAR-01-01