SQL Server中DateDiff() 函数用于返回“开始日期”与“结束日期”之间的差值(如:年、天数等)。
DateDiff() 函数返回值类型为:Long(长整型),范围从 -2,147,483,648 到 2,147,483,647。
DateDiff(参数 , 开始日期 , 结束日期)
参数取值:
| 说明 | 参数写法 | 参数简写 | 最大 |
|---|---|---|---|
| 年 | Year | yy | |
| 季度 | Quarter | q | |
| 月 | Month | m | |
| 周 | Week | wk | |
| 日 | Day | d | |
| 小时 | Hour | hh | |
| 分钟 | Minute | mi | |
| 秒 | Second | s | 68年 |
| 毫秒 | Millisecond | ms | 24天20小时31分23.648秒 |
ASP 实例:
<%
'数据库链接定义省略
sql="SELECT DateDiff(ms,'2017-1-1','2017-1-25 20:31:23.648') AS DiffVal"
Set rs=Server.CreateObject("ADODB.RecordSet")
rs.open sql, conn, 1, 1
if not(rs.bof and rs.eof) then
Response.Write rs("DiffVal")
end if
rs.close
set rs=nothing
'输出:2147483646(毫秒)
sql="SELECT DateDiff(Week,'2016-10-20','2017-10-20') AS DiffVal"
Set rs=Server.CreateObject("ADODB.RecordSet")
rs.open sql, conn, 1, 1
if not(rs.bof and rs.eof) then
Response.Write "<br>"&rs("DiffVal")
end if
rs.close
set rs=nothing
'输出:52(周)
sql="SELECT DateDiff(d,'2016-10-20',GetDate()) AS DiffVal"
Set rs=Server.CreateObject("ADODB.RecordSet")
rs.open sql, conn, 1, 1
if not(rs.bof and rs.eof) then
Response.Write "<br>"&rs("DiffVal")
end if
rs.close
set rs=nothing
'当前系统日期为:2016-10-29,输出:9(天)
%>
声明:如需转载,请注明来源于www.webym.net并保留原文链接:http://www.webym.net/jiaocheng/619.html





















