DATEDIFF function in Oracle(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 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 中的 DATEDIFF 函数
基础教程推荐
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
