开发者

current_date usage

开发者 https://www.devze.com 2023-02-17 12:10 出处:网络
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 m

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'.

0

精彩评论

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

关注公众号