Answer the question
In order to leave comments, you need to log in
How to write a query that displays birthdays?
We need to display people who have a birthday in the next 3 months
We know the date of birth of people
Everything would be fine, but I don’t know how to solve the problem with the transition to the new year because if it’s 1 month now, this is one thing, but if it’s 11 months now, then it’s different and there will be different equations, and I haven’t taken it into account for days. I did
n’t find similar examples in the docks either, I didn’t find any information that would help me.
It would seem a simple task, but I can’t solve it for 5 hours. dead center
Answer the question
In order to leave comments, you need to log in
with birthday as (
select 'Ivanov' as name, date '1977-11-30' as birthdate
union all
select 'Petrov', date '1991-11-18'
union all
select 'Test', date '2000-01-01'
)
select
name, birthdate
from birthday
where
make_date(extract(year from date '2019-11-01')::integer,
extract(month from birthdate)::integer,
extract(day from birthdate)::integer)
between date '2019-11-01' and date '2019-11-01' + interval '3' month
or
make_date(extract(year from date '2019-11-01')::integer + 1,
extract(month from birthdate)::integer,
extract(day from birthdate)::integer)
between date '2019-11-01' and date '2019-11-01' + interval '3' month
You need to look at functions for working with dates, for example date_part. You can also google a ready-made solution, for example, "postgresql birthday query"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question