Get age from record in table: T-SQL
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
精彩评论