CREATE FUNCTION GetVac(@Start datetime,@End datetime)
RETURNS integer AS
BEGIN
declare
@intNum integer,
@intAllDay integer,
@i integer,
@intWeekDay integer,
@tempDateTime datetime,
@intVac integer
select @intAllDay=DateDiff(dd,@Start,@End)
select @i=0
select @intVac=0
while(@i<@intAllDay)
begin
select @tempDateTime=DateAdd(dd,@i,@Start)
select @intWeekDay=datepart(dw,@tempDateTime)-1
if(@intWeekDay=6) or (@intWeekDay=0)
begin
select @intVac=@intVac+1
end
select @i=@i+1
end
return @intVac
END
好的,這個函數生成以後,我們就可以使用這個函數了,比如:
select aa=dbo.GetVac(2000-12-10 00:00:00.000,'2000-12-18 00:00:00.000')
得到的結果是:3 呵呵