简体   繁体   English

几小时的DateDIFF,TIMESTAMPDIFF

[英]DateDIFF, TIMESTAMPDIFF for hours

For the hours I use it as date data type. 我用它作为日期数据类型。 When I subtract it, it just normally deducts. 当我减去它时,它通常会扣除。 How can I subtract it in time format like hours? 如何以时间格式减去它,如小时? I tried put datediff , timestampdiff , but it says invalid identifier . 我试过把datedifftimestampdiff ,但它说invalid identifier Must I create any function before executing? 我必须在执行前创建任何函数吗? Or did I not activate datediff on Oracle? 或者我没有激活Oracle上的datediff

Below is my code: 以下是我的代码:

Select S.Test_Date, E.Testno, S.Examno, S.Serialno, 'Science'
      ,DATEDIFF(hours, F.STARTED, F.ENDED) as hours From Semester S, TIME F
      ,TESTPAPERS e
Where S.Testno= F.Testno
And E.Testno =1;

For timestampdiff: 对于timestampdiff:

Insert Into Test (Test_Date, Testno, Examno, Serialno, Type, Hours)
Select S.Test_Date, E.Testno, S.Examno, S.Serialno, 'Science'
     , TIMESTAMPDIFF(F.STARTED- F.ENDED) as hours From Semester S, TIME F
     , TESTPAPERS e
Where S.Testno= F.Testno
And E.Testno =1;

In Oracle, you can do arithmetic on date/time expressions easily. 在Oracle中,您可以轻松地对日期/时间表达式进行算术运算。 Subtracting one expression from another yields the number of days (with floating-point precision) between the two expressions. 从另一个表达式中减去一个表达式会产生两个表达式之间的天数(具有浮点精度)。

For example, sysdate is right now, sysdate-1 is exactly one day ago, sysdate-(1/24) is one hour ago, and so forth. 例如, sysdate现在是, sysdate-1恰好是一天前, sysdate-(1/24)是一小时前,依此类推。

So if you have two date/time items named when and then , the number of hours between them is (when-then)*24 所以,如果你有一个名为两个日期/时间的项目when ,并then ,它们之间的小时数是(when-then)*24

No need to make it more complex than that if you're staying in Oracle. 如果你留在Oracle中,就不需要让它变得更复杂。 You don't need datediff, which is good because it isn't there. 你不需要约会,这很好,因为它不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM