码迷,mamicode.com
首页 > 数据库 > 详细

DATEDIFF (Transact-SQL)

时间:2015-04-14 12:28:55      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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

 

DATEDIFF (Transact-SQL)

标签:

原文地址:http://www.cnblogs.com/xiguain/p/4424383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!