Office中国论坛/Access中国论坛

标题: [求助]幫我看看這個函數哪個地方有問題? [打印本页]

作者: HG    时间: 2002-11-5 18:34
标题: [求助]幫我看看這個函數哪個地方有問題?
alter function ufn_month_day (@i_ym as char(6))
returns tinyint
as
begin
declare @i_y  char(4),@i_m  char(2),@i_d1  datetime,@i_d2  char(10),@result char(10)
set @i_y =  left(@i_ym,4)
set @i_m =  right(@i_ym,len(@i_ym)-4)
if cast(@i_m as tinyint) < 10
      begin
         set @i_d1 = @i_y +'/'+ '0'+@i_m+'/'+'01'
         set @i_d2 = @i_y +'/'+ '0'+cast(cast(@i_m as tinyint)+1 as char(2))+'/'+'01'
       end
else        
     if cast(@i_m as tinyint) = 12
       begin
           set  @i_m='0'
           set @i_d2 = @i_y +'/'+ cast(cast(@i_m as tinyint)+1 as char(2))+'/'+'01'
        end         
      else
            begin
                   set @i_d1 = @i_y +'/'+@i_m+'/'+'01'
                  set @i_d2 = @i_y +'/'+cast(cast(@i_m as tinyint)+1 as char(2))+'/'+'01'
            end
set @result=datediff(day,cast(@i_d1 as smalldatetime),cast(@i_d2 as smalldatetime))
return (@result)
end
作者: HG    时间: 2002-11-5 18:58
/*
alter function ufn_month_day (@i_ym as varchar(6))
returns tinyint
as
begin
declare @i_y  char(4),@i_m  char(2),@i_d1  datetime,@i_d2  char(10),@result char(10)
set @i_y =  left(@i_ym,4)
set @i_m =  right(@i_ym,(len(@i_ym)-4))
if cast(@i_m as tinyint) < 10
      begin
         set @i_d1 = @i_y +'/'+ '0'+@i_m+'/'+'01'
         set @i_d2 = @i_y +'/'+ '0'+cast(cast(@i_m as tinyint)+1 as char(1))+'/'+'01'
       end
else        
     if cast(@i_m as tinyint) = 12
       begin
           set @i_d1 = @i_y +'/'+@i_m+'/'+'01'
           set  @i_m='01'
           set @i_d2 = cast(cast(@i_y as smallint)+1 as char(4)) +'/'+'01'+'/'+'01'
        end         
      else
            begin
                  set @i_d1 = @i_y +'/'+@i_m+'/'+'01'
                  set @i_d2 = @i_y +'/'+cast(cast(@i_m as tinyint)+1 as char(2))+'/'+'01'
            end
set @result=datediff(day,cast(@i_d1 as smalldatetime),cast(@i_d2 as smalldatetime))
return (@result)
end
*/
如 2002年2月共有多少天
select hrm.dbo.ufn_month_day('20022')
2002年12月共有多少天
select hrm dbo.ufn_month_day('200212')





欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3