Thread: Previous month function

Previous month function

From
"Matt Arnilo S. Baluyos"
Date:
hello everyone,

is there already a built-in function that returns the previous month from the
current timestamp? (and includes functionality to turn back one year if
current month is january)

i don't want to reinvent the wheel but if there's none then i guess i would
just have to make one from scratch.

--
Matt Arnilo S. Baluyos
Research and Software Development
System Net International, Inc. - Philippines

Re: Previous month function

From
Aleksander Kmetec
Date:
You can do something like this:

test=> select NOW() - '1 month'::interval;
            ?column?
-------------------------------
  2004-10-17 12:31:09.944818+02


Simply replace NOW() with your timestamp column.

Regards,
Aleksander



Matt Arnilo S. Baluyos wrote:
> hello everyone,
>
> is there already a built-in function that returns the previous month from the
> current timestamp? (and includes functionality to turn back one year if
> current month is january)
>
> i don't want to reinvent the wheel but if there's none then i guess i would
> just have to make one from scratch.
>

Re: Previous month function

From
Tom Lane
Date:
Aleksander Kmetec <aleksander.kmetec@intera.si> writes:
> You can do something like this:

> test=> select NOW() - '1 month'::interval;
>             ?column?
> -------------------------------
>   2004-10-17 12:31:09.944818+02

Also, depending on just what you meant by "previous month", you might
want to apply date_trunc():

regression=# select date_trunc('month', NOW()) - '1 month'::interval;
        ?column?
------------------------
 2004-10-01 00:00:00-04
(1 row)


            regards, tom lane