日期:2014-05-18 浏览次数:20700 次
--原始数据:@TA declare @TA table(id int,MyType varchar(1),Adate datetime,MyEndDate datetime,MySign int) insert @TA select 1,'R','2007-8-25',null,0 union all select 2,'F','2007-8-2',null,0 union all select 3,'F','2007-8-26',null,0 union all select 4,'F','2007-8-19',null,0 union all select 5,'R','2007-8-19',null,0 union all select 6,'R','2007-8-1',null,0 update @TA set MyEndDate= case (@@datefirst-1+datepart(weekday,Adate))%7 when 0 then MyEndDate else dateadd(day,(case MyType when 'R' then 1 else 3 end)*7-(@@datefirst-1+datepart(weekday,Adate))%7,ADate) end, MySign= case (@@datefirst-1+datepart(weekday,Adate))%7 when 0 then 1 else MySign end select * from @TA /* id MyType Adate MyEndDate MySign ----------- ------ ------------------------------------------------------ ------------------------------------------------------ ----------- 1 R 2007-08-25 00:00:00.000 2007-08-26 00:00:00.000 0 2 F 2007-08-02 00:00:00.000 2007-08-19 00:00:00.000 0 3 F 2007-08-26 00:00:00.000 NULL 1 4 F 2007-08-19 00:00:00.000 NULL 1 5 R 2007-08-19 00:00:00.000 NULL 1 6 R 2007-08-01 00:00:00.000 2007-08-05 00:00:00.000 0 (所影响的行数为 6 行) */
------解决方案--------------------
不好意思,應該是8-
update ta set MyEndDate = (CASE WHEN datepart(dw,Adate)= 1 then MyEndDate
ELSE
CASE WHEN MyType = 'R ' THEN DATEADD(dd,8-DATEPART(dw,Adate),Adate)
CASE WHEN MyType = 'F ' THEN DATEADD(dd,14+8-DATEPART(dw,Adate),Adate) END
END ),
MySign = (CASE WHEN datepart(dw,Aate)= 1 THEN 1 ELSE mySign END )
------解决方案--------------------
declare @TA table(id int,MyType varchar(1),Adate datetime,MyEndDate datetime,MySign int) insert @TA select 1,'R','2007-8-25',null,0 union all select 2,'F','2007-8-2',null,0 union all select 3,'F','2007-8-26',null,0 union all select 4,'F','2007-8-19',null,0 union all select 5,'R','2007-8-19',null,0 union all select 6,'R','2007-8-1',null,0 update t set [MyEndDate]=case when MyType='R' and (DATEPART(Weekday,Adate)+@@DATEFIRST-1)%7<7 then DATEADD(Day,7-(DATEPART(Weekday,Adate)+@@DATEFIRST-1)%7,Adate) when MyType='F' and (DATEPART(Weekday,Adate)+@@DATEFIRST-1)%7<7 then DATEADD(Day,7-(DATEPART(Weekday,Adate)+@@DATEFIRST-1)%7