愿所有的美好和期待都能如约而至

Oracle 中的 DATEDIFF 函数

发布时间:  来源:互联网  作者:匿名  标签:datediff error DATEDIFF function in Oracle exception IT资讯 oracle select sql 技术社区  热度:37.5℃

本文介绍了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

这篇关于Oracle 中的 DATEDIFF 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

勇敢去编程!

勇敢的热爱编程,未来的你一定会大放异彩,未来的生活一定会因编程更好!

TOP