Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOR loop over a date range in Postgres

In one of my functions in Postgres, I am trying to loop over a range of dates using the following code:

FOR timesheet_date IN select generate_series('2012-11-24'::date,'2012-12-03','1 day'::interval)::date LOOP

//My code goes here

END LOOP;

But I am getting an error enter image description here

Now as am getting dates, I think it is not a record variable and hence the error.

But, how can I loop through a date range ? I am very new to Postgres actually.

like image 485
Happy Coder Avatar asked Oct 27 '25 08:10

Happy Coder


1 Answers

DO $$
declare
    dt record;
begin
FOR dt IN SELECT generate_series('2023-02-10'::date, '2023-02-15'::date, '1 day'::interval) LOOP
  RAISE NOTICE 'Processing date: %', dt.generate_series;
END LOOP;
end; $$
like image 161
Anupam Ghosh Avatar answered Oct 30 '25 00:10

Anupam Ghosh



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!