时间类型增加天数,去掉星期六星期天

两个表

表一:

ID BillDate CheckDate

1   2012-04-20 2012-04-27

表二:

ID RelationID Day

1        1        5

RelationID 是和表一ID关联的ID

每次增加表二信息的时候,都根据我填写的Day(天数)来对应更新

CheckDate的字段信息,但是这里主要要考虑到另外一个问题,就是

在增加天数的时候希望不要把星期六星期天给算进去,直接增加。


哔哔one
浏览 792回答 2
2回答

繁星coding

创建一个函数:Create&nbsp;FUNCTION&nbsp;[dbo].[GetWorkDateDiff] (@begin_date&nbsp;datetime,@days&nbsp;int)RETURNS&nbsp;datetimeASBEGINDECLARE&nbsp;@WorkDays&nbsp;int=1declare&nbsp;@weekday&nbsp;Nvarchar(9)Declare&nbsp;@End_Date&nbsp;datetime &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(@WorkDays<@days)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select&nbsp;@weekday=datename(weekday,dateadd(d,@WorkDays,@begin_date))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(@weekday='星期六'&nbsp;or&nbsp;&nbsp;@weekday='星期日'&nbsp;or&nbsp;@weekday='Saturday'&nbsp;or&nbsp;@weekday='Sunday')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set&nbsp;@days=@days+1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set&nbsp;@WorkDays=@WorkDays+1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;RETURN&nbsp;Dateadd(dd,@WorkDays,@Begin_Date)END测试函数:DECLARE&nbsp;@OldDate&nbsp;Datetime='2012-4-24'DECLARE&nbsp;@Days&nbsp;int=5--直接加@Days个工作日SELECT&nbsp;[dbo].[GetWorkDateDiff](@OldDate,@Days)/*2012-05-01&nbsp;00:00:00.000

有只小跳蛙

你循環 增加天數,一天一天增加,直到增加夠你需要的天數。並且每增加一天,判斷是否是星期六或者星期天,如果是,就多增加天,如果不是,就跳過。
打开App,查看更多内容
随时随地看视频慕课网APP