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/*--得到指定日期之间的工作日天数--邹建 2004.06(引用请保留此信息)--*//*--调用示例 select dbo.f_workday('2004-06')
--*/
create function f_workday(
@年月 varchar(7) --要查询的年月,格式为:YYYY-MM
)returns int
as
begin
declare @dt_begin datetime,@dt_end datetime
declare @dt datetime,@re int,@i int
select @dt_begin=@年月+'-01',@dt_end=dateadd(month,1,@dt_begin)-1
select @i=datediff(day,@dt_begin,@dt_end)+1
,@re=@i/7*2
,@dt=dateadd(day,@i/7*7-1,@dt_begin)
while @dt<@dt_end
select @re=case when datepart(weekday,@dt) in(1,7)
then @re+1 else @re end
,@dt=@dt+1
return(@i-@re)
end
go

解决方案 »

  1.   

    好象不大对,declare @aa int
    set @aa= dbo.f_workday('2004-10')
    --set @aa=dbo.f_UDRP_AddNO('BD200405280001',4)
    select @aa
    结果
    ----
    22
    (实际是21天)
      

  2.   

    用文字描述一个自定义函数的算法:天数=4周20个工作日
         加
         case 该月有多少天 
              when 28 then 0  --这个简单,不用说明了。
              when 29 then 查该月29日是星期几,如果是1~5,则为1。
              when 30 then 查该月29日是星期几,如果是1~4,则为2;若5、7,则1。
              when 31 then 查该月29日是星期几,如果是1~3,则为3;若4、7,则为2;其余,则1。
        end