SQL农历转换函数(显示中文格式,加入润月的显示)
if object_id('fn_getlunar') is not null
drop function fn_getlunar
go
create function dbo.fn_getlunar(@solarday datetime)
returns nvarchar(30)
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 @mleap1 int
declare @mleapnum int
declare @bleap smallint
declare @temp int
declare @year nvarchar(10)
declare @month nvarchar(10)
declare @day nvarchar(10)
declare @chinesenum nvarchar(10)
declare @outputdate nvarchar(30)
set @offset=datediff(day,'1900-01-30',@solarday)
--确定农历年开始
set @i=1900
--set @offset=@soldata
while @i<2050 and @offset>0
begin
set @ydays=348
set @mleapnum=0
select @ilunar=dataint from solardata 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 solardata 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
set @mleap1= @mleap
--传回农历年闰月的天数
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=ltrim(@offset)
--输出日期
set @chinesenum='0一二三四五六七八九十'
while len(@year)>0
select @outputdate=isnull(@outputdate,'')
+ substring(@chinesenum,left(@year,1)+1,1)
, @year=stuff(@year,1,1,'')
set @outputdate=@outputdate+'年'
+ case @mleap1 when @month then '润' else '' end
if cast(@month as int)<10
set @outputdate=@outputdate
+ case @month when 1 then '正'
else substring(@chinesenum,left(@month,1)+1,1)
end
else if cast(@month as int)>=10
set @outputdate=@outputdate
+ case @month when '' then '十' when 11 then '十一'
else '十二' end
set @outputdate=@outputdate + '月'
if cast(@day as int)<10
set @outputdate=@outputdate + '初'
+ substring(@chinesenum,left(@day,1)+1,1)
else if @day between '' and ''
set @outputdate=@outputdate
+ case @day when '' then '初十' else '十'+
substring(@chinesenum,right(@day,1)+1,1) end
else if @day between '' and ''
set @outputdate=@outputdate
+ case @day when '' then '二十' else '廿' end
+ case @day when '' then '' else
substring(@chinesenum,right(@day,1)+1,1) end
else
set @outputdate=@outputdate+'三十'
return @outputdate
end
go
select dbo.fn_getlunar(getdate()) as [改编日期(农历)],getdate() as [改编日期(公历)]
SQL农历转换函数(显示中文格式,加入润月的显示)的更多相关文章
- 完善SQL农历转换函数
-------------------------------------------------------------------- -- Author : 原著: 改编:ht ...
- C# 怎么显示中文格式的日期、星期几
//该语句显示的为英文格式DateTime.Now.DayOfWeek.ToString(); //显示中文格式星期几 "星期" + DateTime.Now.ToString(& ...
- sql字符转换函数大全
删除空格 有两个函数,TTRIM()和LTRIM(),可以用来从字符串中剪掉空格.函数LTRIM()去除应该字符串前面的所有空格:函数RTRIM()去除一个字符串尾部的所有空格.这些和vbscript ...
- sql时间转换函数--备忘
总是忘记 一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的 ...
- flutter showDatePicker显示中文日期_Flutter时间控件显示中文
flutter showDatePicker showTimePicker显示中文日期 1.配置flutter_localizations依赖 找到pubspec.yaml配置flutter_loca ...
- 自带日期时间 showDatePicker显示中文日期_Flutter时间控件显示中文
flutter showDatePicker showTimePicker显示中文日期 1.配置flutter_localizations依赖 找到pubspec.yaml配置flutter_loca ...
- sql大小转换函数
将字段值转换成大写 UPDATE t SET [name]=UPPER([name]) 将字段值转换成小写 UPDATE t SET [name]=LOWER([name])
- SQL中format()函数对应的格式
http://www.cnbeta.com/articles/tech/632057.htm
- C# 怎么显示中文格式的星期几
1.DateTime.Now.ToString("dddd",new System.Globalization.CultureInfo("zh-cn")); 2 ...
随机推荐
- 【bzoj2741】[FOTILE模拟赛] L
Portal --> bzoj2741 Solution 突然沉迷分块不能自拔 考虑用分块+可持久化trie来解决这个问题 对于每一块的块头\(L\),预处理\([L,i]\)区间内的所有子区间 ...
- MySQL内存计算器
MySQL如何使用内存? 首先,介绍MySQL使用内存的一些方法: 1. 会话级别的内存消耗(连接私有内存):如sort_buffer_size等,每个会话都会开辟一个sort_buffer_size ...
- java基础-IO流对象之Properties集合
java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...
- 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解
关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...
- bzoj千题计划138:bzoj1432: [ZJOI2009]Function
http://www.lydsy.com/JudgeOnline/problem.php?id=1432 http://blog.sina.com.cn/s/blog_86942b1401014bd2 ...
- java selenium常用API(WebElement、iFrame、select、alert、浏览器窗口、事件、js) 一
WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.clic ...
- EM算法(Expectation Maximization Algorithm)
EM算法(Expectation Maximization Algorithm) 1. 前言 这是本人写的第一篇博客(2013年4月5日发在cnblogs上,现在迁移过来),是学习李航老师的< ...
- 提高PowerShell脚本效率的五个常用方法
PowerShell脚本运行慢怎么办?影响到正常企业流程正常运转怎么办?本文利用例子和数据给大家带来让PowerShell运行更快的五个常用方法.本人拙见,希望能够给大家带来一点启发~ 1. 善用命令 ...
- php中各种hash算法的执行速度比较
更多内容推荐微信公众号,欢迎关注: PHP中的Hash函数很多,像MD4.MD5.SHA-1.SHA-256.SHA-384.SHA-512等我们比较常见,那么各个哈希的执行速度呢? $algos = ...
- 【多视图几何】TUM 课程 第1章 数学基础:线性代数
在 YouTube 上找到了慕尼黑工业大学(Technische Universitaet München)计算机视觉组 Daniel Cremers 教授的 Multiple View Geomet ...