Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

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?

like image 840
Mussie Avatar asked Oct 12 '25 05:10

Mussie


1 Answers

Hers is it:

SELECT date_trunc('quarter', now() - interval '6 month') AS quarter
like image 100
nikhil sugandh Avatar answered Oct 14 '25 21:10

nikhil sugandh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!