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 呵呵