I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Ca开发者_运维问答n some one give me a basic example Please.
You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....
In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.
精彩评论