I have a table that contains birthday date in 3 columns, as bday_day
, bday_month
and bday_year
, With birth_year
nullable
.
I have to write a query to find the upcoming next month birthdays, Even if the birth_year
is null
. (With December to January cases to be taken care)
I have tried the answer given below but got No luck as it is calculated based on year
https://stackoverflow.com/a/18748008/4694871
Edit: I have updated the question from birthdays next month to birthdays in the next 30 days.
Update: Here is what I have tried( with the December to January cases ), Suggestions are welcome..
SELECT to_date(CONCAT(date_part('year',current_date), '-', bday_month, '-',bday_day), 'YYYY-MM-DD') AS birthdate
FROM users u2
WHERE (date(to_date(CONCAT(date_part('year',current_date), '-', bday_month, '-',bday_day), 'YYYY-MM-DD')
+
interval '1 year' * CASE when(DATE_PART('doy',to_date(CONCAT(date_part('year',current_date), '-', bday_month, '-',bday_day), 'YYYY-MM-DD'))
< DATE_PART('doy',current_date)) then 1 else 0 end))
BETWEEN current_date AND date(current_date + INTERVAL '30 day')