sql - Retrieve upcoming birthdays in Postgres -
i have users table dob (date of birth) field, in postgres database.
i want write query retrieve next 5 upcoming birthdays. think following needs considered -
- sorting date of birth won't work because years can different.
- you want result sorted date/month, starting today. so, example, yesterday's date last row.
- ideally, without functions. not deal breaker though.
similar questions have been asked on so, most don't have accepted answer. thank you.
you can @ day of year of dob , compare against current date's doy:
select doy , extract(doy dob) - extract(doy current_date) upcoming_bday users extract(doy dob) - extract(doy current_date) >= 0 order 2 limit 5
Comments
Post a Comment