In the article Dominating dates and times in SQL Server you can find information on how to manipulate dates in SQL Server, including with an item dealing precisely with birthday relationships. From that article I copied the following T-SQL code:
-- código #4.22 v2
-- emite relação de aniversariantes dos próximos dias
--
-- Autor: José Diz
-- Publicado em: Porto SQL - https://portosql.wordpress.com/blog/
-- informa quantidade de dias
declare @NDias tinyint;
set @NDias= 15;
-- gera datas com limites do período de emissão
declare @Amanha date, @Ate date;
set @Amanha= dateadd (day, +1, cast (sysdatetime() as date));
set @Ate= dateadd (day, (@NDias -1), @Amanha);
-- obtém ano inicial e ano final
declare @Ano_I int, @Ano_F int;
set @Ano_I= year (@Amanha);
set @Ano_F= year (@Ate);
--
IF (@Ano_I = @Ano_F)
-- período de emissão no mesmo ano
SELECT Nome, convert (char(5), Data_nasc, 103) as [Dia/Mês]
from dbo.Cadastro
where dateadd (year,
(@Ano_I - year (Data_nasc)),
Data_nasc) between @Amanha and @Ate
order by month (Data_nasc), day (Data_nasc)
else
-- período de emissão cruza ano
SELECT Nome, convert (char(5), Data_nasc, 103) as [Dia/Mês]
from dbo.Cadastro
where dateadd (year,
(@Ano_I - year (Data_nasc)),
Data_nasc) >= @Amanha
or dateadd (year,
(@Ano_F - year (Data_nasc)),
Data_nasc) <= @Ate
order by dateadd (year, (@Ano_I - year (Data_nasc), Data_nasc);
.
What the T-SQL code does is bring the date of birth to the current year (or following year if the period involves more than one year) and then compare with the dates that delimit the period.
In the original SQL code there was no ordering. I added it in this answer but did not test; I hope it is correct.
I think you need a
where
ai– Ricardo Pontual
@Ricardopunctual, already tested too, but it didn’t work. I appreciate your suggestion.
– FABIO Gomez
"based on the current date" and how you’ll filter it if you’re not using it
where
? first Filter that "the next birthday kids until next year", only then order– Ricardo Pontual
@Ricardo Pontual, sorry I ended up not informed, I started studying SQL, yesterday, all I could do was searching a lot, but I still could not assemble the logic with SQL. Thank you, I will continue on the trail of research.
– FABIO Gomez