In this case:
-- código #1 v5
declare @DataInicial date, @DataFinal date;
set @DataInicial = cast(Current_timestamp as date);
set @DataFinal = @DataInicial;
-- verifica se é sexta-feira
set datefirst 7; -- domingo como primeiro dia da semana
IF datepart(weekday, @DataInicial) = 6
set @DataFinal = DateAdd(day, +2, @DataInicial);
--
declare @Ano1 int, @Ano2 int;
set @Ano1= year(@DataInicial);
set @Ano2= year(@DataFinal);
--
IF (@Ano1 = @Ano2)
begin
SELECT NUMERO, Convert(char(5), DATA, 103) as [Dia/Mês]
from utilizadores
where DATA < @DataInicial
and Cast(DateAdd(year, (-year(DATA) + @Ano1), DATA) as date) between @DataInicial and @DataFinal
order by month(DATA), day(DATA)
end
else
IF (@Ano1 < @Ano2)
begin
SELECT NUMERO, Convert(char(5), DATA, 103) as [Dia/Mês]
from utilizadores
where DATA < @DataInicial
and (Cast(DateAdd(year, (-year(DATA) + @Ano1), DATA) as date) >= @DataInicial
or Cast(DateAdd(year, (-year(DATA) + @Ano2), DATA) as date) <= @DataFinal)
order by month(DATA), day(DATA)
end;
I chose to use variables of type date to define the period of issue, as this makes it easier to deal with month and year changes. The user’s date of birth is updated to the year of the issuing period and then it is checked if it is in the period.
Assuming the period of issue is 1/2/2017 to 10/2/2017, and if the number 8 user was born in "9/19/1960", the date is transformed into "9/19/2017" and then compared with the period limitation dates:
se "19/9/2017" está entre "1/2/2017" e "10/2/2017"
então "é aniversariante";
What is the purpose of the consultation? // You cite "this query" but in the description there is no T-SQL code. // To facilitate the proposition of answers, I suggest you add information about the table and the columns directly involved in this query, as well as example.
– José Diz
make an annual listing... with dates ranges of days closed by the user
– usersantos