如题,
请高手帮忙一下,应该有人遇到过这个问题吧?
感谢!
------解决方案--------------------
if exists (select * from dbo.sysobjects where id = object_id(N '[dbo].[f_WorkDay] ') and xtype in (N 'FN ', N 'IF ', N 'TF '))
drop function [dbo].[f_WorkDay]
GO
--计算两个日期相差的工作天数
CREATE FUNCTION f_WorkDay(
@dt_begin datetime, --计算的开始日期
@dt_end datetime --计算的结束日期
)RETURNS int
AS
BEGIN
DECLARE @workday int,@i int,@bz bit,@dt datetime
IF @dt_begin> @dt_end
SELECT @bz=1,@[email protected]_begin,@[email protected]_end,@[email protected]
ELSE
SET @bz=0
SELECT @i=DATEDIFF(Day,@dt_begin,@dt_end)+1,
@[email protected]/7*5,
@dt_begin=DATEADD(Day,@i/7*7,@dt_begin)
WHILE @dt_begin <[email protected]_end
BEGIN
SELECT @workday=CASE
WHEN (@@DATEFIRST+DATEPART(Weekday,@dt_begin)-1)%7 BETWEEN 1 AND 5
THEN @workday+1 ELSE @workday END,
@[email protected]_begin+1
END
RETURN(CASE WHEN @bz=1 THEN [email protected] ELSE @workday END)
END
GO
------解决方案--------------------
create function 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
while dateadd(d,@i,@begin) <[email protected]
begin
if datepart(weekday,dateadd(d,@i,@begin)) not in(1,7)
set @[email protected]+1
set @[email protected]+1
end
end
return @j
END