Home > Software engineering >  Subtract hours from current date time and compare to column value whose data type is datetime
Subtract hours from current date time and compare to column value whose data type is datetime

Time:03-19

I want all the MAFN column values that were added to the table in the last hour(:mm). The column ReturnDt stores the time it was added in DateTime format ex.'2005-01-11 08:50:24.767'

I can get the hours for ReturnDt and get the current hour(in two separate queries).

SELECT  ReturnDt, 
       DATEPART(hour, ReturnDt) As 
FROM [Sidney].[dbo].[LibraryTransactions]

And

SELECT GETDATE() 'Today', DATEPART(hh,GETDATE()) 'Hour Part'

But this is not enough for obvious reasons. Firstly SELECT GETDATE()... doesn't work as a subquery. Secondly, I can't seem to subtract 1 hour from the result of the SELECT GETDATE() query. Thirdly, even if the above two somehow worked, the queries reduce time to hours and it doesn't take into account the date.

I am using SQl Server 2005

Thanks in advance!

Edit:

The table looks like: enter image description here

Expected Result: If I ran the query today(18 Mar 2022) at 16:00 hours I should only get these values enter image description here

CodePudding user response:

How about DateDiff()

where
   DateDiff( hour, ReturnDT, getdate() ) > 1
  • Related