开发者

Adding only certain days to a DateAdd in SQL?

I've got an inventory report I'm working with which calculates our average weekly consumption of a particular part and then divides it by what we have on hand to show us a numerical "weeks on hand". So开发者_运维百科 if we have an average weekly consumption of 496 and we have 1500 pieces available, the weeks on hand should be 3.02.

What I'm trying to do is take that number and calculate exactly which day we'll run out of the part in question. So we'd run out of the example part in 3.02 weeks from now. My issue is that I have to exclude Fridays - Sundays, so each work week is only four days.

I've looked at the DATEADD() function, and this seems to be what I need to use, but I'm just not sure how to exclude the Fridays from it. I'm using SQL 2005 for this.

Many thanks!


DECLARE @old_dt SMALLDATETIME
SET @old_dt = GETDATE()
DECLARE @new_dt SMALLDATETIME
SET @new_dt = DATEADD(d,CAST(3.2*7 AS INT),@old_dt)
SELECT CASE WHEN DATENAME(weekday,@new_dt) = 'Thursday' THEN DATEADD(d,2,@new_dt) WHEN  DATENAME(weekday,@new_dt) = 'Friday' THEN DATEADD(d,1,@new_dt) ELSE @new_dt END
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜