Calculate difference between 2 date / times in Oracle SQL(计算 Oracle SQL 中 2 个日期/时间之间的差异)
问题描述
我有一个表格如下:
Filename - varchar
Creation Date - Date format dd/mm/yyyy hh24:mi:ss
Oldest cdr date - Date format dd/mm/yyyy hh24:mi:ss
如何计算 Oracle SQL 中两个日期之间的小时、分钟和秒(可能还有天)的差异?
How can I calcuate the difference in hours minutes and seconds (and possibly days) between the two dates in Oracle SQL?
谢谢
推荐答案
您可以在 Oracle 中减去日期.这会给您带来天数的差异.乘以 24 得到小时,依此类推.
You can substract dates in Oracle. This will give you the difference in days. Multiply by 24 to get hours, and so on.
SQL> select oldest - creation from my_table;
如果您的日期存储为字符数据,则必须先将其转换为日期类型.
If your date is stored as character data, you have to convert it to a date type first.
SQL> select 24 * (to_date('2009-07-07 22:00', 'YYYY-MM-DD hh24:mi')
- to_date('2009-07-07 19:30', 'YYYY-MM-DD hh24:mi')) diff_hours
from dual;
DIFF_HOURS
----------
2.5
<小时>
注意:
此答案适用于由 Oracle 数据类型 DATE 表示的日期.Oracle 还有一个数据类型TIMESTAMP,它也可以表示一个日期(带时间).如果你减去 TIMESTAMP 值,你会得到一个 INTERVAL;要提取数值,请使用 EXTRACT 函数.
This answer applies to dates represented by the Oracle data type DATE.
Oracle also has a data type TIMESTAMP, which can also represent a date (with time). If you subtract TIMESTAMP values, you get an INTERVAL; to extract numeric values, use the EXTRACT function.
这篇关于计算 Oracle SQL 中 2 个日期/时间之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:计算 Oracle SQL 中 2 个日期/时间之间的差异
基础教程推荐
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在多列上分布任意行 2021-01-01
- oracle区分大小写的原因? 2021-01-01
