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 函数


基础教程推荐
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01