SQL获取两个日期的时间间隔

DATEDIFF()函数
DATEDIFF(datepart,startdate,enddate)

获取两个日期之间的天数
SELECT DATEDIFF(day,'2008-06-05','2009-08-10') AS days;
SELECT DATEDIFF(day,'2008-08-05 08:00:00','2008-08-10 09:00:00') AS days;

获取两个日期之间的年数
SELECT DATEDIFF(year,'2008-06-05','2009-08-10') AS DiffDate;
SELECT DATEDIFF(year,'2008-08-05 08:00:00','2008-08-10 09:00:00') AS DiffDate;

上一篇:希尔排序的递归写法


下一篇:SQL Server中的Datediff函数