Home  >  Article  >  Database  >  DateDiff函数

DateDiff函数

WBOY
WBOYOriginal
2016-06-07 15:58:552231browse

在MySQL中可以使用DATEDIFF()函数计算两个日期之间的天数 语法: datediff(date1,date2) 注:date1和date2需是合法的日期或日期/时间表达式 例1 SELECT DATEDIFF(2010-6-30,2010-6-26) AS DiffDate 例2 SELECT DATEDIFF(20140618145239,20140613125249) AS Di

在MySQL中可以使用DATEDIFF()函数计算两个日期之间的天数
语法:
datediff(date1,date2)
注:date1和date2需是合法的日期或日期/时间表达式

例1

SELECT DATEDIFF('2010-6-30','2010-6-26') AS DiffDate
\

例2

SELECT DATEDIFF('20140618145239','20140613125249') AS DiffDate
\

oracle中没有DateDiff函数 如何计算两个日期之间的天数呢?

在oracle中日期类型可以直接比较

select to_date('2014-05-05','yyyy-mm-dd') - to_date('2014-05-02','yyyy-mm-dd') from dual
\

这样就可以计算出两个时间相差的天数。

如果是时间戳

SELECT to_date('20140322154628', 'yyyymmddhh24miss') - to_date('20140320114631', 'yyyymmddhh24miss')
from dual 
\

 也可以计算相差天数,但发现有小数,所以可以使用floor

select floor(to_date('20140322154628', 'yyyymmddhh24miss') - to_date('20140320114631', 'yyyymmddhh24miss')) from dual; 
\

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn