码迷,mamicode.com
首页 > 数据库 > 详细

SQL 字符 数字 转换字母

时间:2015-07-09 12:52:08      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

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

 

SQL 字符 数字 转换字母

标签:

原文地址:http://www.cnblogs.com/nimeide/p/4632687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!