如何从Oracle中的日期中减去小时数,这样也会影响日期 [英] How to subtract hours from a date in Oracle so it affects the day also

查看:521
本文介绍了如何从Oracle中的日期中减去小时数,这样也会影响日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Oracle中减去日期,因此它甚至也会影响日期.例如,如果 时间戳记为01/June/2015 00小时,如果我减去2小时,我希望能够转到2014年5月31日22小时.

I'm trying to subtract date from Oracle so it even effect the day as well. For example, if the timestamp is 01/June/2015 00 hours and if I subtract 2 hours, I want to be able to go to to 31/May/2014 22 hours.

我尝试过

to_char(sysdate-(2/11), 'MM-DD-YYYY HH24')

但是只减去小时;它不会触及当天本身.

but it only subtracts the hour; it does not touch the day itself.

推荐答案

其他人对2/11的使用(不正确)进行了评论,以指定所需的时间间隔.

Others have commented on the (incorrect) use of 2/11 to specify the desired interval.

但是我个人更喜欢使用ANSI interval文字编写类似的内容,这使得读取查询更加容易:

I personally however prefer writing things like that using ANSI interval literals which makes reading the query much easier:

sysdate - interval '2' hour

它也具有可移植的优点,许多DBMS都支持此功能.另外,我不必启动计算器就可以知道表达式所表示的小时数-我对心理算术非常不满意;)

It also has the advantage of being portable, many DBMS support this. Plus I don't have to fire up a calculator to find out how many hours the expression means - I'm pretty bad with mental arithmetics ;)

这篇关于如何从Oracle中的日期中减去小时数,这样也会影响日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆