SQL Server Create a filter in a DateTime field, bringing the previous 24 hours (not counting the current time)

0

Good evening, I'm having trouble putting a date filter, I have a query where I bring the information of CD, Crane and DtRegister (datetime) In this query I am already filtering the 24 hours between the current day and 24 hours before,

GRU_DT_REGISTRO between GETDATE()-1 and GETDATE()

In this filter I try to set it to bring me to from the current date and time, the 24 hours before, however it brings data from 25 hours, or 22:00 today until 22 yesterday, I would like it to display the previous 24 hours without counting the current DateTime, for example are now: 06/08/2017 22:15:00, so in my filter should have dates between 05/08/2017 22:00 & 08/08/2017 21:59:00 Can someone give me some help:

I'm using Sql Server 2014

    
asked by anonymous 07.08.2017 / 03:16

1 answer

0

You can use the function DATEADD() to return 1 day before the exact time the filter was run. The function takes 3 arguments:

  • The range ex: (day, month, year and etc);
  • The number of days ex: (minus negative for subtraction, positive for addition);
  • Reference date;
  • Try this:

    GRU_DT_REGISTRO BETWEEN DATEADD(day, -1, GETDATE()) AND GETDATE()
    

    You can use CAST to return only the date:

    GRU_DT_REGISTRO BETWEEN CAST(DATEADD(day, -1, GETDATE()) AS DATE) AND CAST(GETDATE() AS DATE)
    
        
    19.02.2018 / 01:50