zoukankan      html  css  js  c++  java
  • SQL 字符 数字 转换字母

    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
  • 相关阅读:
    python学习笔记:遍历目录
    c++笔记:友元函数
    VMware Workstation 9: This virtual machine's policies are too old to be run by this version of VMware
    inet_ntoa内存问题
    python学习笔记:sqlite3查询
    python学习笔记:利用asyncore的端口映射(端口转发)
    编写谷歌浏览器的油猴脚本
    window编译7z
    通过配置nginx的header路由到不同环境的服务器
    用U盘给物理机安装ubuntu20.04
  • 原文地址:https://www.cnblogs.com/nimeide/p/4632687.html
Copyright © 2011-2022 走看看