SQL 字符 数字 转换字母

非 Y 不嫁゛ 提交于 2020-03-08 17:09:23

1、将字符中数字转换为中文大写的数字,

注意

100==>一零零   而非==>一百

123==>一二三   而非==>一百二十三

CREATE FUNCTION Fn_NumberConvertChinase(@number NVARCHAR(50))
RETURNS NVARCHAR(20)
AS
BEGIN
DECLARE @res NVARCHAR(20)
DECLARE @char CHAR(1)
SET @res = ''
SET @char = substring(@number,1,1)
SELECT @res = (CASE @char
    WHEN '1' THEN '一'
    WHEN '2' THEN '二'
    WHEN '3' THEN '三'
    WHEN '4' THEN '四'
    WHEN '5' THEN '五'
    WHEN '6' THEN '六'
    WHEN '7' THEN '七'
    WHEN '8' THEN '八'
    WHEN '9' THEN '九'
    WHEN '0' THEN '零'
    ELSE substring(@number,1,1) END 
)
IF(len(@number) > 1)
BEGIN
    SET @res =  @res + dbo.fn_numberconvertchinase(substring(@number,2,len(@number)-1))
END
RETURN @res 
END

2、获取字符首字母,并转大写

CREATE FUNCTION Fn_GetPy(@str NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
    DECLARE @strlen INT;
    DECLARE @re NVARCHAR(4000);
    --字符中数字转大写
    SELECT @str=dbo.fn_numberconvertchinase(@str);
    
    DECLARE @t table(chr nchar(1) collate Chinese_PRC_CI_AS,letter nchar(1))
    INSERT INTO @t(chr,letter)
      SELECT '吖','A' UNION ALL SELECT '八','B' UNION ALL
      SELECT '嚓','C' UNION ALL SELECT '咑','D' UNION ALL
      SELECT '妸','E' UNION ALL SELECT '发','F' UNION ALL
      SELECT '旮','G' UNION ALL SELECT '铪','H' UNION ALL
      SELECT '丌','J' UNION ALL SELECT '咔','K' UNION ALL
      SELECT '垃','L' UNION ALL SELECT '嘸','M' UNION ALL
      SELECT '拏','N' UNION ALL SELECT '噢','O' UNION ALL
      SELECT '妑','P' UNION ALL SELECT '七','Q' UNION ALL
      SELECT '呥','R' UNION ALL SELECT '仨','S' UNION ALL
      SELECT '他','T' UNION ALL SELECT '屲','W' UNION ALL
      SELECT '夕','X' UNION ALL SELECT '丫','Y' UNION ALL
      SELECT '帀','Z';
      SELECT @strlen=len(@str),@re='';
  
      WHILE @strlen>0
      BEGIN
        SELECT TOP 1 @re=letter+@re,@strlen=@strlen-1
          FROM @t a WHERE chr<=substring(@str,@strlen,1)
          ORDER BY chr DESC
        IF @@rowcount=0
          SELECT @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
      END
  RETURN(UPPER(@re))
END

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!