TIMESTAMPDIFF in Oracle 11g?

假如想象 提交于 2021-01-07 03:11:24

问题


I've seen documentation for a TIMESTAMPDIFF function in Oracle here, but it mentions it's for "Oracle9i Lite".

When I try to use the example on Oracle 11g:

SELECT TIMESTAMPDIFF(SQL_TSI_DAY, CURRENT_DATE, '1998-12-09') FROM DUAL;

I get an invalid identifier error on TIMESTAMPDIFF. Is this feature not available in Oracle 11g? Or is it something my DBA needs to configure?


回答1:


That function doesn't exist in Oracle RDMBS; it clearly did in Oracle Lite (which I've never encountered) and I believe it's in other databases like MySQL.

There is documentation covering datetime and interval arithmetic which will get you part of the way there; you can either subtract one timestamp from another; or one date from another, or a mix of the two - but it's simpler to stick to one data type. Depending which you use you'll either get an interval or a number representing the number of days:

SQL> SELECT CURRENT_TIMESTAMP - TIMESTAMP '1998-12-09 00:00:00' FROM DUAL;

CURRENT_TIMESTAMP-TIMESTAMP'1998-12-0900:00:00'
---------------------------------------------------------------------------
+000006169 16:16:21.287166000

SQL> SELECT CURRENT_DATE - DATE '1998-12-09' FROM DUAL;

CURRENT_DATE-DATE'1998-12-09'
-----------------------------
                   6169.67775

If you just want the number of whole days you can use extract() for the interval to get just the element you want, or trunc() for the number to remove the fractional part:

SQL> SELECT EXTRACT(DAY FROM CURRENT_TIMESTAMP - TIMESTAMP '1998-12-09 00:00:00') FROM DUAL;

EXTRACT(DAYFROMCURRENT_TIMESTAMP-TIMESTAMP'1998-12-0900:00:00')
---------------------------------------------------------------
                                                           6169

SQL> SELECT TRUNC(CURRENT_DATE - DATE '1998-12-09') FROM DUAL;

TRUNC(CURRENT_DATE-DATE'1998-12-09')
------------------------------------
                                6169

You could also trunc() the current date before comparison if you prefer, so you're comparing both at midnight, which means there won't be a fractional day part to remove:

SQL> SELECT TRUNC(CURRENT_DATE) - DATE '1998-12-09' FROM DUAL;

TRUNC(CURRENT_DATE)-DATE'1998-12-09'
------------------------------------
                                6169

I've used ANSI date literals for the fixed date, but you can use an existing date or timestamp variable or column; or to_date() or to_timestamp() if you have a string in a different format.


You can also use extract() to convert an interval's components to a combined value, as shown here; and also make sure you're aware of the difference between current_date and sysdate, and the timestamp equivalents.




回答2:


I've never encountered TIMESTAMPDIFF, but you can accomplish something similar by simply applying regular math to your date values:

SELECT CURRENT_DATE - to_timestamp('1998-12-09','yyyy-mm-dd') FROM DUAL;


来源:https://stackoverflow.com/questions/33440030/timestampdiff-in-oracle-11g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!