SqlServer⽇期转汉字字符串以下脚本转⾄互联⽹,增加了⾃⼰需要的功能并改成了函数的⽅式
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: 互联⽹
-- Create date: 2020年3⽉9⽇
-- Description: 数字转中⽂字符串
-- =============================================
CREATE function [dbo].[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
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-
- =============================================
-- Author: 互联⽹
-- Create date: 2020年3⽉9⽇
-- Description: ⽇期转中⽂字符串
-- =============================================
CREATE function [dbo].[ConvertDateToChinese]
(@vdate datetime,@NoDay int)
returns varchar(50)
as
begin
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 = vertNumToChinese(substring(@vYear,1,1))
set @temstr = @temstr + vertNumToChinese(substring(@vYear,2,1))
set @temstr = @temstr + vertNumToChinese(substring(@vYear,3,1))
set @temstr = @temstr + vertNumToChinese(substring(@vYear,4,1))
set @temstr = @temstr + '年'
-- 开始计算⽉份
if substring(@vMonth,1,1) = '0'
set @temstr = @temstr + vertNumToChinese(substring(@vMonth,2,1))
else
begin
if substring(@vMonth,2,1) = '0'
set @temstr = @temstr + '⼗'
else
set @temstr = @temstr +'⼗'+ vertNumToChinese(substring(@vMonth,2,1))
end
set @temstr = @temstr + '⽉'
if (@NoDay=1)
begin
-- 开始计算⽇期
if convert(int,@vDay) < 10
set @temstr = @temstr +  vertNumToChinese(substring(@vDay,1,1))
else
begin
if substring(@vDay,2,1) = '0'
begin
if  substring(@vDay,1,1)<> '1'
set  @temstr = @temstr +  vertNumToChinese(substring(@vDay,1,1)) + '⼗'
else
set @temstr = @temstr +  '⼗'
end
else
begin
if substring(@vDay,1,1) <> '1'
set @temstr = @temstr  +  vertNumToChinese(substring(@vDay,1,1)) + '⼗' + vertNumToChinese(substring(@vDay,2,1))    else
set @temstr = @temstr + '⼗' + vertNumToChinese(substring(@vDay,2,1))
end
end
set @temstr = @temstr +  '⽇'
end
return @temstr
end
GO
--调⽤⽅式有两种
--获取截⽌⽉份的字符串
字符串截取日期select dbo.ConvertDateToChinese(GETDATE(),0)
--获取完整⽇期字符串
select dbo.ConvertDateToChinese(GETDATE(),1)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。