DATEDIFF (Transact-SQL)
dataDiff 多用于获取指定时间间隔内的数据集,如:
select count(1) as col2 from ComplaintMain cm where DateDiff(dd,cm.CreatedOn,getDate())<=7 and cm.HandlingBy=" + (int)HandlingBy.MemberCD + @" and cm.CState=" + (int)ComplaintState.Successfully + ";";
上面的query是获取一周内条件数据;
函数方法:
DATEDIFF ( datepart , startdate , enddate )
datepart参数:
datepart |
Abbreviations |
---|---|
year |
yy, yyyy |
quarter |
qq, q |
month |
mm, m |
dayofyear |
dy, y |
day |
dd, d |
week |
wk, ww |
hour |
hh |
minute |
mi, n |
second |
ss, s |
millisecond |
ms |
microsecond |
mcs |
nanosecond |
ns |
demo:
SELECT DATEDIFF(year, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(quarter, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(month, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(dayofyear, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(day, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(week, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(hour, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(minute, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(second, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘); SELECT DATEDIFF(millisecond, ‘2005-12-31 23:59:59.9999999‘ , ‘2006-01-01 00:00:00.0000000‘);
例子:
CREATE TABLE dbo.Duration ( startDate datetime2 ,endDate datetime2 ); INSERT INTO dbo.Duration(startDate,endDate) VALUES(‘2007-05-06 12:10:09‘,‘2007-05-07 12:10:09‘); SELECT DATEDIFF(day,startDate,endDate) AS ‘Duration‘ FROM dbo.Duration; -- Returns: 1
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。