Search Postgresql Archives

Re: getting last day of month

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You could try :

SELECT int4(EXTRACT( DAYS FROM CURRENT_DATE + '1 month'::interval -
CURRENT_DATE ));

----------------------------------------------------------------------------
--------------- 
Patrick Fiche 
email : patrick.fiche@xxxxxxxxxxx 
tel : 01 69 29 36 18 
----------------------------------------------------------------------------
--------------- 




-----Original Message-----
From: pgsql-general-owner@xxxxxxxxxxxxxx
[mailto:pgsql-general-owner@xxxxxxxxxxxxxx]On Behalf Of Sergey Pariev
Sent: jeudi 25 aout 2005 18:44
To: pgsql-general postgresql.org
Subject:  getting last day of month


Hi all.
I need to find out the last day of current month. Currently I do the 
trick with code below, but that's rather ugly way to do it IMHO. Could 
anybody suggest me a better way ?

The following is my testing procedure :

CREATE or REPLACE FUNCTION test_findout_dates()
RETURNS integer AS $$
DECLARE
    begin_date date;
    end_date date;
    current_month int;
    current_year int;
    last_day int;
 BEGIN
    current_month := extract ( month from now() ) ;
    current_year := extract ( year from now() ) ;

    begin_date := current_year || '-' || current_month || '-01' ;

    last_day := 31;
    begin
        end_date := (current_year || '-' || current_month || '-'|| 
last_day) :: date;
        last_day := 0 ;
    exception
        when others then
            raise notice '31 doesnt cut for month %',current_month ;
        end;

    if last_day > 0 then
        begin
            last_day := 30;
            end_date := (current_year || '-' || current_month || '-'|| 
last_day) :: date;
            last_day := 0 ;
        exception
                when others then
                raise notice '30 doesnt cut for month %',current_month ;
        end;
    end if;

    if last_day > 0 then
        begin
            last_day := 29;
            end_date := (current_year || '-' || current_month || '-'|| 
last_day) :: date;
            last_day := 0 ;
        exception
                when others then
                raise notice '29 doesnt cut for month %',current_month ;
        end;
    end if;

    if last_day > 0 then
        begin
            last_day := 28;
            end_date := (current_year || '-' || current_month || '-'|| 
last_day ) :: date;
            last_day := 0 ;
        exception
                when others then
                raise notice '28 doesnt cut for month %',current_month ;

        end;
    end if;

    raise notice 'begin date is % ',begin_date;
    raise notice 'end date is % ',end_date;

    return 1;
 END;
$$ LANGUAGE plpgsql ;

Thans in Advance, Sergey.


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux