Skip to content
Advertisement

Can I start the quarter of the year from the month I wanted to on PostgreSQL?

I am using PostgreSQL 14.5

I can classify the year by quarter using the Postgres keyword QUARTER as illustrated in the documentation just like SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');. And it works. The default quarter starts with January. Which makes complete sense. I.e, quarter one is composed of January, February, and March. The others follow then. But, I wanted to make the month configurable so that the quarter can start at any month.

Eg. If the month starts in April, April, May, and July would be the first quarter.

I have started writing my custom function to come up with a solution but,

Can I do this without creating a custom function?

Advertisement

Answer

Hers is it:

SELECT date_trunc('quarter', now() - interval '6 month') AS quarter
9 People found this is helpful
Advertisement