日期:2014-05-17 浏览次数:20545 次
SET DATEFIRST 1;
SELECT *,datepart(dw,completeddate),datepart(dw,createdate),
convert(varchar(12),dateadd(DAY,1-datepart(dw,completeddate)+7,completeddate),111)+' 9:30',datepart(WEEK,completeddate),datepart(WEEK,createdate)
FROM dbo.csdn1
WHERE completeddate<=createdate AND
datepart(dw,completeddate)>4
AND
( (datepart(WEEK,createdate) -datepart(WEEK,completeddate))=1
OR
(datepart(WEEK,createdate)=1 AND datepart(WEEK,completeddate)>1) --跨年
)
AND (datepart(dw,completeddate)>=1 OR datepart(dw,createdate)<=5)
AND createdate >=convert(varchar(20),dateadd(DAY,1-datepart(dw,completeddate),completeddate)+7,111)+' 9:30'
IF(OBJECT_ID('TA','U') IS NOT NULL) DROP TABLE TA
CREATE TABLE TA(completeddate DATETIME,createdate DATETIME)
INSERT INTO TA
SELECT '2012-11-19 11:30','2012-11-20 10:00'
UNION ALL SELECT '2012-11-23 11:30','2012-11-26 09:00'
UNION ALL SELECT '2012-11-16 11:30','2012-11-17 09:00'
SELECT createdate,completeddate,
CASE DATEPART(weekday,Ndate) WHEN 5 THEN DATEADD(D,3,A.Ndate)
WHEN 6 THEN DATEADD(D,2,A.Ndate)
WHEN 7 THEN DATEADD(D,1,A.Ndate)
ELSE Ndate END AS Ndate
FROM (
SELECT createdate,completeddate,
DATEADD(d,1,convert(varchar(10),completeddate,120))+'9:30' AS Ndate FROM TA )AS A
WHERE A.createdate< Ndate
create table t1
(
completeddate datetime,
createdate datetime
)
insert into t1 values('2012-11-19 11:30','2012-11-20 10:00:00')
insert into t1 values('2012-11-23 11:30','2012-11-26 09:00:00')
insert into t1 values('2012-11-23 11:30','2012-11-30 09:40