零-点

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以下脚本转至互联网,增加了自己需要的功能并改成了函数的方式

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 = 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 (@NoDay=1)
begin
-- 开始计算日期
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 +  '日'  
end
return @temstr
end 
GO

  

 --调用方式有两种
 --获取截止月份的字符串
 select dbo.ConvertDateToChinese(GETDATE(),0)
 --获取完整日期字符串
 select dbo.ConvertDateToChinese(GETDATE(),1)
posted on 2020-03-10 17:37  零-点  阅读(808)  评论(0编辑  收藏  举报