开发者

Get age from record in table: T-SQL

开发者 https://www.devze.com 2023-03-01 16:19 出处:网络
So each record on my table has a datetime timestamp column. How do I return the age (in hours), of these records in the datab开发者_运维知识库ase?

So each record on my table has a datetime timestamp column.

How do I return the age (in hours), of these records in the datab开发者_运维知识库ase?

Thank you!


select DATEDIFF(hour,timestamp,getdate()) as hours_old
from myTable


datediff(hour,[Your Column], getdate())

http://msdn.microsoft.com/en-us/library/ms189794.aspx


Use the datediff function.

select datediff(hour, getdate(), dob) as age
   from ...


Since datediff(hour,'2000-01-01 00:59','2000-01-01 01:00') returns 1, due to the (counterintuitive) way datediff works, you may want something more accurate:

select DATEDIFF(minute,the_timestamp,getdate()) / 60
  from TheTable
0

精彩评论

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