SQL Server DATEDIFF() 函数

超、凢脫俗 2021-12-12 03:05 460阅读 0赞

Definition and Usage

The DATEDIFF() function returns the time between two dates.

Syntax

DATEDIFF(datepart,startdate,enddate)

Where startdate and enddate are valid date expressions and datepart can be one of the following:




























































datepart Abbreviation
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns

Example

Now we want to get the number of days between two dates.

We use the following SELECT statement:

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

Result:

61

Example

Now we want to get the number of days between two dates (notice that the second date is “earlier” than the first date, and will result in a negative number).

We use the following SELECT statement:

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

Result:

-61

转载于:https://www.cnblogs.com/TSPWater/archive/2010/01/14/1647791.html

发表评论

表情:
评论列表 (有 0 条评论,460人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SQL Server聚合函数

    在本教程中,将了解和学习SQL Server聚合函数以及如何使用它们来计算聚合。 聚合函数执行一个或多个值的计算并返回单个值。 聚合函数通常与[SELECT][]语句的[GR