try this:
declare @date datetime;
set @date='2012-08-09'
SELECT case when datepart(weekday, @date) >5 then
DATEADD(DAY, +4, DATEADD(WEEK, DATEDIFF(WEEK, 0, @date), 0))
else DATEADD(DAY, -3, DATEADD(WEEK, DATEDIFF(WEEK, 0, @date), 0)) end
result:
2012-08-03
Example2:
declare @date datetime;
set @date='2012-08-10'
SELECT case when datepart(weekday, @date) >5 then
DATEADD(DAY, +4, DATEADD(WEEK, DATEDIFF(WEEK, 0, @date), 0))
else DATEADD(DAY, -3, DATEADD(WEEK, DATEDIFF(WEEK, 0, @date), 0)) end
result:
2012-08-10
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…