IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[lunar]') AND type = 'FN')
DROP FUNCTION [dbo].[lunar]
GO
 
create function  [dbo].[lunar](@solarday  datetime)          
returns  datetime         
as          
begin          
   declare  @soldata  int          
   declare  @offset  int          
   declare  @ilunar  int          
   declare  @i  int            
   declare  @j  int            
   declare  @ydays  int          
   declare  @mdays  int          
   declare  @mleap  int          
   declare  @mleapnum  int          
   declare  @bleap  smallint          
   declare  @temp  int          
         
   declare  @year  int            
   declare  @month  int          
   declare  @day  int          
             
   declare  @outputdate  datetime          
     
   --保证传进来的日期是不带时间          
   set  @solarday=cast(@solarday  as  char(10))          
   set  @offset=cast(@solarday-'1900-01-30'  as  int)      
     
         
   --确定农历年开始          
   set  @i=1900          
   --set  @offset=@soldata          
   while  @i<2050  and  @offset>0          
   begin          
       set  @ydays=348          
       set  @mleapnum=0          
       select  @ilunar=dataint  from  ebs_public.dbo.pub_solardata with(nolock)  where  yearid=@i          
         
       --传回农历年的总天数          
       set  @j=32768          
       while  @j>8          
       begin          
           if  @ilunar  &  @j  >0          
               set  @ydays=@ydays+1          
           set  @j=@j/2          
       end          
         
       --传回农历年闰哪个月  1-12  ,  没闰传回  0          
       set  @mleap  =  @ilunar  &  15          
         
       --传回农历年闰月的天数  ,加在年的总天数上          
       if  @mleap  >  0          
       begin          
           if  @ilunar  &  65536  >  0          
               set  @mleapnum=30          
           else            
               set  @mleapnum=29          
         
           set  @ydays=@ydays+@mleapnum          
       end          
                 
       set  @offset=@offset-@ydays          
       set  @i=@i+1          
   end          
             
   if  @offset  <=  0          
   begin          
       set  @offset=@offset+@ydays          
       set  @i=@i-1          
   end          
   --确定农历年结束              
   set  @year=@i          
     
   --确定农历月开始          
   set  @i  =  1          
   select  @ilunar=dataint  from  ebs_public.dbo.pub_solardata with(nolock)   where  yearid=@year      
     
   --判断那个月是润月          
   set  @mleap  =  @ilunar  &  15          
   set  @bleap  =  0        
     
   while  @i  <  13  and  @offset  >  0          
   begin          
       --判断润月          
       set  @mdays=0          
       if  (@mleap  >  0  and  @i  =  (@mleap+1)  and  @bleap=0)          
       begin--是润月          
           set  @i=@i-1          
           set  @bleap=1          
           --传回农历年闰月的天数          
           if  @ilunar  &  65536  >  0          
               set  @mdays  =  30          
           else            
               set  @mdays  =  29          
       end          
       else          
       --不是润月          
       begin          
           set  @j=1          
           set  @temp  =  65536            
           while  @j<=@i          
           begin          
               set  @temp=@temp/2          
               set  @j=@j+1          
           end          
         
           if  @ilunar  &  @temp  >  0          
               set  @mdays  =  30          
           else          
               set  @mdays  =  29          
       end          
             
       --解除闰月      
       if  @bleap=1  and  @i=  (@mleap+1)      
           set  @bleap=0      
     
       set  @offset=@offset-@mdays          
       set  @i=@i+1          
   end          
         
   if  @offset  <=  0          
   begin          
       set  @offset=@offset+@mdays          
       set  @i=@i-1          
   end          
     
   --确定农历月结束              
   set  @month=@i      
         
   --确定农历日结束              
   set  @day=@offset          
         
   set  @outputdate=cast((cast(@year  as  varchar(4))+'-'+cast(@month  as  varchar(2))+'-'+cast(@day  as  varchar(2)))  as  datetime)          
   return  @outputdate      
end