I am looking for an optimal decision of how to get table values according to their date and time but within just ONE past hour.
I mean something in this way (a pseudocode):
SELECT value FROM Table WHERE date BETWEEN getdate() AND getdate()-ONE_HOUR
For the purpose of this question Table
has these columns:
value
date
Any useful snippet is appreciated :)
How to add Hours to DateTime in Sql Server? We can use DATEADD() function like below to add hours to DateTime in Sql Server. DATEADD() functions first parameter value can be hour or hh all will return the same result.
We can use DATEPART() function to get the HOUR part of the DateTime in Sql Server, here we need to specify datepart parameter of the DATEPART function as hour or hh.
SELECT Value
FROM Table
WHERE Date between dateadd(hour, -1, getdate()) and getdate()
Description of the DATEADD
function:
DATEADD (datepart , number , date )
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
datepart Abbreviations
----------- -------------
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
More information:
Something like this should work.
SELECT value
FROM Table
WHERE date >= dateadd(hour,-1,getdate())
and date <= getdate()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With