SQL Server DATEDIFF() 函数

SQL 教程 · 2019-03-04 14:43:09

定义和用法

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

语法

DATEDIFF(datepart,startdate,enddate)

startdate 和 enddate 参数是合法的日期表达式。datepart 参数可以是下列的值:

datepart 缩写
yy, yyyy
季度 qq, q
mm, m
年中的日 dy, y
dd, d
wk, ww
星期 dw, w
小时 hh
分钟 mi, n
ss, s
毫秒 ms
微妙 mcs
纳秒 ns

实例

现在我们想要获取两个日期之间的天数。

我们使用下面的 SELECT 语句:

SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate

结果:

DiffDate
61

实例

现在我们想要获取两个日期之间的天数(请注意,第二个日期比第一个日期早,结果将是一个负数)。

我们使用下面的 SELECT 语句:

SELECT DATEDIFF(day,'2008-08-05','2008-06-05') AS DiffDate

结果:

DiffDate
-61

点击查看所有 SQL 教程 文章: https://codercto.com/courses/l/29.html

查看所有标签

Modeling the Internet and the Web

Modeling the Internet and the Web

Pierre Baldi、Paolo Frasconi、Padhraic Smyth / Wiley / 2003-7-7 / USD 115.00

Modeling the Internet and the Web covers the most important aspects of modeling the Web using a modern mathematical and probabilistic treatment. It focuses on the information and application layers, a......一起来看看 《Modeling the Internet and the Web》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具