slandau
slandau

Reputation: 24102

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 database?

Thank you!

Upvotes: 1

Views: 709

Answers (4)

brianary
brianary

Reputation: 9332

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

Upvotes: 0

Richard Schneider
Richard Schneider

Reputation: 35464

Use the datediff function.

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

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28894

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

Upvotes: 2

turtlepick
turtlepick

Reputation: 2714

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

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

Upvotes: 2

Related Questions