如何从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 ;)