Oracle 中的 DATEDIFF 函数

2021-12-06 00:00:00 sql select oracle datediff

我需要使用 Oracle,但 DATEDIFF 函数在 Oracle DB 中不起作用.

I need to use Oracle but DATEDIFF function doesn't work in Oracle DB.

如何在Oracle中编写以下代码?我看到了一些使用 INTERVAL 或 TRUNC 的示例.

How to write the following code in Oracle? I saw some examples using INTERVAL or TRUNC.

SELECT DATEDIFF ('2000-01-01','2000-01-02') AS DateDiff;

推荐答案

在 Oracle 中,您可以简单地减去两个日期并获得 天 的差值.另请注意,与 SQL Server 或 MySQL 不同,在 Oracle 中,您不能在没有 from 子句的情况下执行 select 语句.解决这个问题的一种方法是使用内置的虚拟表,dual:

In Oracle, you can simply subtract two dates and get the difference in days. Also note that unlike SQL Server or MySQL, in Oracle you cannot perform a select statement without a from clause. One way around this is to use the builtin dummy table, dual:

SELECT TO_DATE('2000-01-02', 'YYYY-MM-DD') -  
       TO_DATE('2000-01-01', 'YYYY-MM-DD') AS DateDiff
FROM   dual

相关文章