万年历SQL Server中实现

参照C#版万年历实现而做
http://www.cnblogs.com/txw1958/archive/2013/01/27/csharp-calendar.html


在sql server中实现公历和农历的转换例子:

调用公历转农历

exec getLunarAndSolarDate 1995,10,2,8,2,0,1

solarDt solarYsolarM solarDlunarDtStr isLeapYisLeapM curJQprevJQ nextJQnGan nZhiyGan yZhirGan rZhisGan sZhiconsteName animalchinaConstellation SolarHolidayLunarHoliday WeekDayHolidayWeek
1995-10-02 08:02:00.000 199510 2 农历一九九五年闰八月初八日 11 秋分 白露 寒露 天秤座 房日兔国庆节假日 NULL国际住房日 星期一

调用农历转公历

exec getLunarAndSolarDate 1995,8,8,8,2,1,0


solarDt solarYsolarM solarDlunarDtStr isLeapYisLeapM curJQprevJQ nextJQnGan nZhiyGan yZhirGan rZhisGan sZhiconsteName animalchinaConstellation SolarHolidayLunarHoliday WeekDayHolidayWeek
1995-10-02 00:00:00.000 199510 2 农历一九九五年闰八月初八日 11 秋分 白露 寒露 天秤座 房日兔国庆节假日 NULL国际住房日 星期一


exec getLunarAndSolarDate 1995,8,8,8,2,0,0

solarDt solarY solarM solarD lunarDtStr isLeapY isLeapM curJQ prevJQ nextJQ nGan nZhi yGan yZhi rGan rZhi sGan sZhi consteName animal chinaConstellation SolarHoliday LunarHoliday WeekDayHoliday Week
1995-09-02 00:00:00.000 1995 9 2 农历一九九五年八月初八日 1 0 处暑 立秋 白露 狮子座 亢金龙 NULL NULL NULL 星期六


ALTER FUNCTION [dbo].[fBitShift]
(
	@i int = 1 ,-- integer
	@n int,  -- shift
	@left bit -- if left shift
)
RETURNS int
AS
BEGIN
	 declare @m int,@s int
	 if @left = 0 
	 select  @n%=32,@m=power(2,31-@n),@s=@i&@m,@i&=@m-1,@i*=power(2.,@n)
	 else 
	 select  @n%=32,@m=power(2,31-@n),@s=@i&@m,@i&=@m-1,@i/=power(2.,@n)
	 if(@s>0)set @i|=0x80000000
	 return @i -- -1382285312
END


ALTER FUNCTION [dbo].[fConvertLunarDtStr] 
(
	@n int,
	@type int -- 1:year 2:month 3:day
)
RETURNS nvarchar(10)
AS
BEGIN
  declare @lStr nvarchar(10),@HZNum  nvarchar(10),@nStr1 nvarchar(10),@nStr2 nvarchar(4),@nStr3 nvarchar(13)
  set @HZNum = '零一二三四五六七八九'
  set @nStr1 = N'日一二三四五六七八九'
  set @nStr2 = N'初十廿卅'

  if @type =1 and (@n <1 or @n >9) set @lStr=''
  else if @type =2 and (@n <1 or @n >13) set @lStr=''
  else if @type =3 and (@n <1 or @n >30) set @lStr=''
  else if @type =3
	  begin
	   if @n = 10 set @lStr='初十'
	   else if @n = 20 set @lStr='二十'
	   else if @n = 30 set @lStr='三十'
	   else set @lStr=substring(@nStr2,@n/10+1,1)+substring(@nStr1,@n%10+1,1)
	  end
  else 
  begin
     if (@n <10) set @lStr=substring(@HZNum,@n+1,1)
	 if @type =2 and @n = 1 set @lStr='正'
	 if @n = 10 set @lStr='十'
	 if @n = 11 set @lStr&#
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值