eugene
eugene

Reputation: 41745

SQL, How to create series of dates between given two dates?

Suppose you have a cron-like expression

2:30 every monday and wednesday and a period 2020.05.23 - 2021.02.02

I want to create a row for every matching time in the period

2:30 2020.05.25 (mon)
2:30 2020.05.27 (wed)

How to create them in batches?

(I'm on postgresql, django if it matters)

Upvotes: 0

Views: 55

Answers (1)

Nameless
Nameless

Reputation: 523

To get only the timestamps you want you can use following query

select d from
    generate_series('2020.05.23 2:30:00'::timestamp, '2021.02.02 2:30:00'::timestamp, '1 day'::interval) d
where extract(dow from d) in (1,3)

To insert into table in batch

Insert into <table_name> (<column_name>)
select d from
    generate_series('2020.05.23 2:30:00'::timestamp, '2021.02.02 2:30:00'::timestamp, '1 day'::interval) d
where extract(dow from d) in (1,3)

Upvotes: 1

Related Questions