Error performing fetch by date greater than 1 year DATADIFF () and DATAADD () function

0

Hello, I need to make a select in a database where the databuffer is older than one year. However I need to use a clause and in my where and when I try to use the DATEDIFF () and DATEADD () function in this way it does not return anything. Is there any limitation on these functions or my select that is incorrect?

select tb_Pedido.idPedido, 
        numeroPedido,dataCompra 
   from tb_Pedido, tb_NotaFiscal
     where DATEDIFF(dd,dataCompra, getdate()) > 366 AND
       tb_Pedido.idPedido = tb_NotaFiscal.idPedido AND
       tb_NotaFiscal.ARMAZENADO =0;

With dateadd ()

select tb_Pedido.idPedido,numeroPedido,dataCompra  
  from tb_Pedido, tb_NotaFiscal
   where  
      tb_Pedido.dataCompra <= 
      DATEADD(yyyy,-1,getdate())
      AND tb_Pedido.idPedido = tb_NotaFiscal.idPedido AND
      tb_NotaFiscal.ARMAZENADO =0;

But when I use only the where without the clause and it works, however I need and to do a check.

    
asked by anonymous 26.09.2018 / 19:16

1 answer

0

The difference between the two methods is that the first method uses days , and in the second it returns a date with one year . < strong> leap years there is a difference in the criterion effectively. On the other hand GetDate () also returns the time.

select DateDiff(dd,'2017-09-28','2018-09-28') 

365

Select DateAdd(yyyy,-1,getdate()) 

'2017-09-28 10:09:00'

    
28.09.2018 / 09:33