Postgres birthdays selection

后端 未结 11 1340
猫巷女王i
猫巷女王i 2020-12-10 08:31

I work with a Postgres database. This DB has a table with users, who have a birthdate (date field). Now I want to get all users who have their birthday in the upcoming week.

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 08:52

    I know this post is old, but I had the same issue and came up with this simple and elegant solution: It is pretty easy with age() and accounts for lap years... for the people who had their birthdays in the last 20 days:

    SELECT * FROM c 
    WHERE date_trunc('year', age(birthdate)) != date_trunc('year', age(birthdate + interval '20 days'))
    

提交回复
热议问题