在MSSQL中将数字转换成中文

具体代码如下:

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)

结果:

  二十一

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。