sql-servergetdate

How to select data using getdate() + time?


I have this code:

select * 
from Table
where [Date] >= cast(getdate() - 1 as date) 
  and [Date] < cast(getdate() + 1 as date)
order by [Date] desc

and I need to change this part:

where [Date] >= cast(getdate() - 1 as date)

that the search start at 02:00 AM + getdate() - 1,

Something like this:

where [Date] >= getdate() - 1, 02:00:00

Solution

  • In SQL Server with the datetime type you can simply concatenate the time portion after first removing it. Try

    where [Date] > DateAdd(dd, DateDiff(dd, 1, GetDate()), 0) + '02:00:00'