目录

SQL服务器 DATEDIFF() 函数

示例

返回两个日期值之间的差值(以年为单位):

SELECT DATEDIFF(year, '2017/08/25', '2011/08/25') AS DateDiff;
亲自试一试 »

定义和用法

DATEDIFF() 函数返回两个日期之间的差异。

语法

DATEDIFF( interval, date1, date2)

参数值

Parameter Description
interval Required. The part to return. Can be one of the following values:
  • year, yyyy, yy = Year
  • quarter, qq, q = Quarter
  • month, mm, m = month
  • dayofyear = Day of the year
  • day, dy, y = Day
  • week, ww, wk = Week
  • weekday, dw, w = Weekday
  • hour, hh = hour
  • minute, mi, n = Minute
  • second, ss, s = Second
  • millisecond, ms = Millisecond
date1, date2 Required. The two dates to calculate the difference between

技术细节

工作于: SQL Server(从 2008 年开始)、Azure SQL 数据库、Azure SQL 数据仓库、并行数据仓库

更多示例

示例

返回两个日期值之间的差异(以月为单位):

SELECT DATEDIFF(month, '2017/08/25', '2011/08/25') AS DateDiff;
亲自试一试 »

示例

返回两个日期值之间的差异(以小时为单位):

SELECT DATEDIFF(hour, '2017/08/25 07:00', '2017/08/25 12:45') AS DateDiff;
亲自试一试 »