开发者

T-SQL (date) - how to get value within one hour?

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_H开发者_开发技巧OUR

For the purpose of this question Table has these columns:

  • value
  • date

Any useful snippet is appreciated :)


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:

  • DATEADD (Transact-SQL)


Something like this should work.

SELECT value
FROM Table 
WHERE date >= dateadd(hour,-1,getdate())
   and date <= getdate()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜