简体   繁体   English

如何使用 PostgreSQL 确定上个月的最后一天?

[英]How do I determine the last day of the previous month using PostgreSQL?

I need to query a PostgreSQL database to determine records that fall within today's date and the last day of the previous month.我需要查询一个 PostgreSQL 数据库来确定属于今天日期和上个月最后一天的记录。 In other words, I'd like to retrieve everything that falls between December 31, 2011 and today.换句话说,我想检索 2011 年 12 月 31 日到今天之间的所有内容。 This query will be re-used each month, so next month, the query will be based upon the current date and January 31, 2012.这个查询每个月都会重复使用,所以下个月,查询将基于当前日期和 2012 年 1 月 31 日。

I've seen this option , but I'd prefer to avoid using a function (if possible).我见过这个选项,但我宁愿避免使用函数(如果可能的话)。

Both solutions include the last day of the previous month and also include all of "today".两种解决方案都包括上个月的最后一天,还包括所有“今天”。

For a date column:对于date列:

SELECT *
FROM   tbl
WHERE  my_date BETWEEN date_trunc('month', now())::date - 1
               AND     now()::date

You can subtract plain integer values from a date (but not from a timestamp ) to subtract days.您可以从date (但不能从timestamp )中减去纯整数值以减去天数。 This is the simplest and fastest way.这是最简单、最快的方法。

For a timestamp column:对于timestamp列:

SELECT *
FROM   tbl
WHERE  my_timestamp >= date_trunc('month', now()) - interval '1 day'
AND    my_timestamp <  date_trunc('day'  , now()) + interval '1 day'

I use the < operator for the second condition to get precise results (read: "before tomorrow").我将<运算符用于第二个条件以获得精确的结果(阅读:“明天之前”)。

I do not cast to date in the second query.我没有在第二个查询中强制转换为date Instead I add an interval '1 day' , to avoid casting back and forth.相反,我添加了一个interval '1 day' ,以避免来回转换。

Have a look at date / time types and functions in the manual.查看手册中的 日期/时间类型功能

For getting date of previous/last month:获取上/上个月的日期

SELECT (date_trunc('month', now())::date - 1) as last_month_date

Result: 2012-11-30结果:2012-11-30

For getting number of days of previous/last month:获取上个月/上个月的天数

SELECT DATE_PART('days', date_trunc('month', now())::date - 1) last_month_days

Result: 30结果:30

Try this:尝试这个:

SELECT ...
WHERE  date_field between (date_trunc('MONTH', now()) - INTERVAL '1 day')::date 
                      and now()::date 
       ...

take from http://wiki.postgresql.org/wiki/Date_LastDay , and modified to return just the days in a month取自http://wiki.postgresql.org/wiki/Date_LastDay ,并修改为仅返回一个月中的天数

    CREATE OR REPLACE FUNCTION calc_days_in_month(date)
    RETURNS double precision AS
    $$
      SELECT EXTRACT(DAY FROM (date_trunc('MONTH', $1) + INTERVAL '1 MONTH - 1         day')::date);
    $$ LANGUAGE 'sql' IMMUTABLE STRICT;


    select calc_days_in_month('1999-05-01')

returns 31返回 31

Reference is taken from this blog: 参考来自这个博客:

You can use below function:您可以使用以下功能:

CREATE OR REPLACE FUNCTION fn_GetLastDayOfMonth(DATE)
RETURNS DATE AS
$$
    SELECT (date_trunc('MONTH', $1) + INTERVAL '1 MONTH - 1 day')::DATE;
$$ LANGUAGE 'sql' 
IMMUTABLE STRICT;

Sample executions:示例执行:

SELECT *FROM fn_GetLastDayOfMonth(NOW()::DATE);

尝试

select current_date - cast((date_part('day', current_date) + 1) as int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM