Birthday listing in a date range

Asked

Viewed 379 times

0

You are giving me an error when I do this query to compare the day and month of anniversary in sql format(data,'MM-dd') . need to take from date the month and year and make a between

Select NUMERO,DATA from utilizadores where  format(DATA ,'MM-dd') between '12-30' and '01-02'

I intend to make a birthday listing the format does not work in some versions of sql

Another mistake is when the year changes

  • 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.

  • make an annual listing... with dates ranges of days closed by the user

2 answers

1

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";
  • the date is in this format 1960-09-19 00:00:00.000

  • @usersants: Indicates that the DATE column is declared as datetime. Tested #1 v3?

  • that’s right it works

  • Basically it will serve to list the annual today, in case it is a Friday presents the Saturday and Sunday. Only one thing this Dateadd(year, (-year(date) + @Ano1) function is for)

  • @usersantos - I changed the #1 code to get the current date and automatically set the final date, considering whether it is Friday or not. // The question changes the year of birth to the year of the deadline, allowing the direct comparison of interval. For example, it transforms "9/19/1960" into "9/19/2017" and checks if it is in the emission period.

-1

Try to filter year and month as example below!

SET DATEFORMAT DMY;

DECLARE
    --@ANO  INT;
    @MES    INT;

--SET   @ANO    = 2017;
SET @MES    =   2; -- fevereiro


SELECT
    U.NUMERO,
    U.DATA

FROM
    UTILIZADORES U

WHERE
    -- DATEPART(YEAR, U.DATA) = @ANO
    DATEPART(MONTH, U.DATA) = @MES;
  • the format gives me error. in some versions of sql.

  • the datepart puts month 2 only... missing the 02 when then I will compare

  • I didn’t understand it right.... I edited the answer! You can choose the year and month you want.

  • If changing the year doesn’t work it only gives for one day

  • I edited... filtering only the month for any year.

  • I want to have a listing between two dates... day and month

  • if you do from one year to the next

  • For day, use datepar(day, data);

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.