也是今天sql群里有人问,看起来这个问题挺简单,但是我不知道具体该怎么实现。百度了一把,找到一个高手贡献的答案,记一下。
参考链接 sql中转换中文日期
------ 配合相关函数 ------
create function convertNumToChinese
(@instr varchar(2))
returns varchar(2)
as
begin
declare @temStr varchar(2)
if @instr = '' set @temStr = '一'
if @instr ='' set @temStr = '二'
if @instr ='' set @temStr = '三'
if @instr ='' set @temStr = '四'
if @instr ='' set @temStr = '五'
if @instr ='' set @temStr = '六'
if @instr ='' set @temStr = '七'
if @instr ='' set @temStr = '八'
if @instr ='' set @temStr = '九'
if @instr ='' 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) = ''
set @temstr = @temstr + dbo.convertNumToChinese(substring(@vMonth,2,1))
else
begin
if substring(@vMonth,2,1) = ''
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) = ''
begin
if substring(@vDay,1,1)<> ''
set @temstr = @temstr + dbo.convertNumToChinese(substring(@vDay,1,1)) + '十'
else
set @temstr = @temstr + '十'
end
else
begin
if substring(@vDay,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
调用方法:
declare @str_format varchar(30);
exec convertDateToChinese '2017-11-08 13:30',@str_format output
select @str_format
结果: