Skip to content

Instantly share code, notes, and snippets.

@dominiceden
Last active November 6, 2016 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominiceden/476f5ab3be5b7165b43532ff77f559c5 to your computer and use it in GitHub Desktop.
Save dominiceden/476f5ab3be5b7165b43532ff77f559c5 to your computer and use it in GitHub Desktop.
Get dates for a particular date range and a day of the week in PostgreSQL
# Monday is 1
# Tuesday is 2
# Wednesday is 3
# Thursday is 4
# Friday is 5
# Saturday is 6
# Sunday is 7
# Note - the Postgres isodow function as used below gives us Monday - Sunday as 0-7. The dow function does Sunday-Monday, 0-6.
# Here we get all of Friday's appointments in a given date range and for a given barber_id.
Appointment.find_by_sql(SELECT * from appointments WHERE (barber_id = 1 AND EXTRACT(isodow FROM start_time) IN (5) AND start_time >= '2016-11-11 06:00:00' AND end_time <= '2016-11-18 10:00:00'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment