具体代码如下:
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')
结果:
二十一