sqlserver年月日转汉字大写

Posted jiafeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlserver年月日转汉字大写相关的知识,希望对你有一定的参考价值。

也是今天sql群里有人问,看起来这个问题挺简单,但是我不知道具体该怎么实现。百度了一把,找到一个高手贡献的答案,记一下。

参考链接 sql中转换中文日期

------ 配合相关函数 ------
create  function convertNumToChinese
(@instr varchar(2))
returns varchar(2)
as
begin
declare  @temStr varchar(2)
if @instr = \'1\' set @temStr =  \'\'
if @instr =\'2\'  set @temStr =  \'\'
if @instr =\'3\'  set @temStr =  \'\'
if @instr =\'4\'  set @temStr =  \'\'
if @instr =\'5\'  set @temStr =  \'\'
if @instr =\'6\'  set @temStr =  \'\'
if @instr =\'7\'  set @temStr =  \'\'
if @instr =\'8\'  set @temStr =  \'\'
if @instr =\'9\'  set @temStr =  \'\'
if @instr =\'0\'  set @temStr =  \'\'
return @temstr
end 

--------------实现关键--存储过程--------------------
create procedure convertDateToChinese
@vdate datetime,@ChineseDateStr varchar(50) out
as
declare @vYear varchar(20)
declare @vMonth varchar(20)
declare @vDay varchar(20)
declare @temstr varchar(100)
 
set @temstr = \'\'
set @vYear = datename(yyyy,@vdate)
--print @vyear
set @vMonth = datename(mm,@vdate)
--print @vMonth
set @vDay  = datename(dd,@vdate)
--print \'v\'+@vDay
-- 开始计算年份
set @temstr = dbo.convertNumToChinese(substring(@vYear,1,1))
set @temstr = @temstr + dbo.convertNumToChinese(substring(@vYear,2,1))
set @temstr = @temstr + dbo.convertNumToChinese(substring(@vYear,3,1))
set @temstr = @temstr + dbo.convertNumToChinese(substring(@vYear,4,1))
set @temstr = @temstr + \'\'
 
-- 开始计算月份
if substring(@vMonth,1,1) = \'0\'
set @temstr = @temstr + dbo.convertNumToChinese(substring(@vMonth,2,1))
else
begin
 if substring(@vMonth,2,1) = \'0\'
   set @temstr = @temstr + \'\'
 else
   set @temstr = @temstr +\'\'+ dbo.convertNumToChinese(substring(@vMonth,2,1))
end
 
set @temstr = @temstr + \'\'
 
-- 开始计算日期
 
if convert(int,@vDay) < 10 
  set @temstr = @temstr +  dbo.convertNumToChinese(substring(@vDay,1,1))
else
begin
  if substring(@vDay,2,1) = \'0\'
    begin 
     if  substring(@vDay,1,1)<> \'1\'
       set  @temstr = @temstr +  dbo.convertNumToChinese(substring(@vDay,1,1)) + \'\'
      else
        set @temstr = @temstr +  \'\'
     end
  else
    begin
     if substring(@vDay,1,1) <> \'1\'
       set @temstr = @temstr  +  dbo.convertNumToChinese(substring(@vDay,1,1)) + \'\' + dbo.convertNumToChinese(substring(@vDay,2,1))
     else
        set @temstr = @temstr + \'\' + dbo.convertNumToChinese(substring(@vDay,2,1))
    end
end
set @temstr = @temstr +  \'\'  
print @temstr
set @ChineseDateStr = @temstr
View Code

调用方法:

declare @str_format varchar(30);
exec convertDateToChinese \'2017-11-08 13:30\',@str_format output
select @str_format

结果:

 

以上是关于sqlserver年月日转汉字大写的主要内容,如果未能解决你的问题,请参考以下文章

java 数字转大写汉字

数字金额转大写汉字

转一个财务方面常用到的数字金额转成汉字大写金额 php类

阿拉伯数字 转 汉字大写

将数字金额转成汉字大写的

sql函数--汉字转拼音(亲测sqlserver可用)