先创建一个函数计算时间差
Alter function dbo.fn_test(@begin datetime,@end datetime)
returns int
As
BEGIN
declare @i int,@j int
set @i=0
set @j=0
if @end<@begin
begin
declare @t datetime
set @t=@end
set @end=@begin
set @begin=@t
end
while dateadd(hh,@i,@begin) <@end
begin
if datepart(weekday,dateadd(hh,@i,@begin)) not in(1,7)
set @j=@j+1
set @i=@i+1
end
return @j
end
调用函数
select dbo.fn_test(@begin,@end)