--断定夏历年入手下手
SET@i=1900
--SET@offset=@solData
WHILE@i<2050AND@offset>0
BEGIN
SET@yDays=348
SET@mLeapNum=0
SELECT@iLunar=dataIntFROMSolarDataWHEREyearId=@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
WHILE@i<13AND@offset>0
BEGIN
--判别润月
SET@mDays=0
IF(@mLeap>0AND@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=1AND@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=convert(varchar(10),CAST((CAST(@YEARASVARCHAR(4))+-+CAST(@MONTHASVARCHAR(2))+-+CAST(@DAYASVARCHAR(2)))ASDATETIME),120)
selectconvert(varchar(10),@solarDay,120)as阳历
,cast(dbo.f_num_str(year(@OUTPUTDATE))asvarchar(8))+年|+
casewhendatalength(dbo.f_num_str(month(@OUTPUTDATE)))=4thencasewhenleft(dbo.f_num_str(month(@OUTPUTDATE)),1)一
thenleft(month(@OUTPUTDATE),1)elseend+十+casewhenright(dbo.f_num_str(month(@OUTPUTDATE)),1)=零thenelseright(dbo.f_num_str(month(@OUTPUTDATE)),1)endelse
cast(dbo.f_num_str(month(@OUTPUTDATE))asvarchar(4))end+月|
+casewhendatalength(dbo.f_num_str(day(@OUTPUTDATE)))=4thencasewhencast(dbo.f_num_str(day(@OUTPUTDATE))asvarchar(4))=一零then初else(casewhenleft(cast(dbo.f_num_str(day(@OUTPUTDATE))asvarchar(4)),1)一
thenleft(cast(dbo.f_num_str(day(@OUTPUTDATE))asvarchar(4)),1)elseend)end+十+casewhenright(cast(dbo.f_num_str(day(@OUTPUTDATE))asvarchar(4)),1)=零thenelse
right(cast(dbo.f_num_str(day(@OUTPUTDATE))asvarchar(4)),1)endelse初+cast(dbo.f_num_str(day(@OUTPUTDATE))asvarchar(4))endas农历
GO
--注重:存储过程当中利用了自界说函数f_num_str
--加此函数的目标是把数字换年夜写
CREATEFUNCTION[dbo].[f_num_str](@numint)
RETURNSvarchar(100)
AS
BEGIN
DECLARE@n_strVARCHAR(20),@reVARCHAR(20),@iint
SELECT@n_str=cast(@numasvarchar),@i=1,@re=
WHILE@i<=len(@n_str)
BEGIN
SET@re=@re+SUBSTRING(零一二三四五六七八九,CAST(SUBSTRING(@n_str,@i,1)ASint)+1,1)
SET@i=@i+1
END
RETURN@re
END