具体代码如下:

CREATE FUNCTION [dbo].[fn_NumberToChinese] (@number INT)
RETURNS VARCHAR(20)
AS
    BEGIN
        DECLARE @res VARCHAR(20);
        DECLARE @str VARCHAR(10);
        DECLARE @char CHAR(1);
        SET @res = \'\';
        SET @str = CAST(@number AS VARCHAR);
        SET @char = SUBSTRING(@str, 1, 1);
        SELECT  @res = ( CASE ( CAST(@char AS INT) )
                           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 \'\'
                           ELSE \'\'
                         END );
        IF ( LEN(@str) > 1 )
            BEGIN
                SELECT  @res = @res + ( CASE LEN(@str)
                                          WHEN 2 THEN \'\'
                                          WHEN 3 THEN \'\'
                                          WHEN 4 THEN \'\'
                                          WHEN 5 THEN \'\'
                                          WHEN 6 THEN \'\'
                                          WHEN 7 THEN \'\'
                                          ELSE \'\'
                                        END );
                SET @res = @res
                    + dbo.fn_NumberToChinese(CAST(SUBSTRING(@str, 2,
                                                              LEN(@str) - 1) AS INT));
            END;
        RETURN @res; 
END;

使用:

SELECT dbo.fn_NumberToChinese(\'21\')

结果:

  二十一

版权声明:本文为zuqing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zuqing/p/4564619.html