Home > Database > Mysql Tutorial > body text

DateDiff函数

WBOY
Release: 2016-06-07 15:58:55
Original
2237 people have browsed it

在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)
Copy after login
注:date1和date2需是合法的日期或日期/时间表达式

例1

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

例2

SELECT DATEDIFF('20140618145239','20140613125249') AS DiffDate
Copy after login
\

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

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

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

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

如果是时间戳

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

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

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

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template