T-SQL函数将数字转换为单词?

时间:2018-08-31 15:50:34

标签: sql-server string function tsql

我正在努力构建一个可以将字符串中的数字0-9转换为它的拼写单词的函数。这是我到目前为止的内容,我意识到“单词”不是内置转换,而这正是我对如何构造它的想法:

CREATE FUNCTION [dbo].[fn_Numbers2Words]
    (@strText VARCHAR(1000))
RETURNS varchar(1000)
AS
BEGIN
    WHILE PATINDEX('%[0-9]%', @strText) > 0
    BEGIN
        SET @strText = STUFF(@strText, PATINDEX('%[0-9]%', @strText), 1, CONVERT(word, PATINDEX('%[0-9]%', @strText)))
    END
    RETURN @strText
END

想法是输入如下内容:

  SELECT [dbo].[fn_Numbers2Word]('1900testnumber')

并返回:

  'oneninezerozerotestnumber'

我已经尝试过执行整数运算的函数,但是由于我的字符串将包含字母,因此它们不起作用。我也尝试过将这些函数合并到上面的函数中,但是没有运气。我确定这只是我在语法上的明智选择。

任何人都可以帮助我更改上述功能,以便产生所需的结果吗?

3 个答案:

答案 0 :(得分:6)

由于您只想进行简单的替换,因此使用嵌套替换将变得非常快捷和简单。我会避免在这里使用标量函数。

declare @strText varchar(1000) = '1900testnumber'

select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@strText, '1', ' one'), '2', 'two'), '3', 'three'), '4', 'four'), '5', 'five'), '6', 'six'), '7', 'seven'), '8', 'eight'), '9', 'nine'), '0', 'zero')

答案 1 :(得分:1)

只是为了好玩,您可以在函数中添加另一个选项

示例

Declare @S varchar(max) = '1900testnumber'

Select @S = replace(@S,sFrom,sTo)
 From (values ('0','zero')
             ,('1','one')
             ,('2','two')
             ,('3','three')
             ,('4','four')
             ,('5','five')
             ,('6','six')
             ,('7','seven')
             ,('8','eight')
             ,('9','nine')
      ) A(sFrom,sTo)

Select @S

返回

oneninezerozerotestnumber

答案 2 :(得分:0)

CHOOSE强行使用:

  

从SQL Server的值列表中返回指定索引处的项目。

DECLARE @num INT = 20;

SELECT CHOOSE(@num,
'one ',
'two ',
'three ',
'four ',
'five ',
'six ',
'seven ',
'eight ',
'nine ',
'ten',
'eleven ',
'twelve ',
'thirteen ',
'fourteen ',
'fifteen ',
'sixteen ',
'seventeen ',
'eighteen ',
'nineteen ',
'twenty',
'twenty-one ',
'twenty-two ',
'twenty-three ',
'twenty-four ',
'twenty-five ',
'twenty-six ',
'twenty-seven ',
'twenty-eight ',
'twenty-nine ',
'thirty',
'thirty-one ',
'thirty-two ',
'thirty-three',
'thirty-four ',
'thirty-five ',
'thirty-six ',
'thirty-seven ',
'thirty-eight ',
'thirty-nine ',
'forty',
'forty-one ',
'forty-two ',
'forty-three ',
'forty-four ',
'forty-five ',
'forty-six ',
'forty-seven ',
'forty-eight ',
'forty-nine ',
'fifty',
'fifty-one ',
'fifty-two ',
'fifty-three ',
'fifty-four ',
'fifty-five ',
'fifty-six ',
'fifty-seven ',
'fifty-eight ',
'fifty-nine ',
'sixty',
'sixty-one ',
'sixty-two ',
'sixty-three ',
'sixty-four ',
'sixty-five ',
'sixty-six ',
'sixty-seven ',
'sixty-eight ',
'sixty-nine ',
'seventy',
'seventy-one ',
'seventy-two ',
'seventy-three ',
'seventy-four ',
'seventy-five ',
'seventy-six ',
'seventy-seven ',
'seventy-eight ',
'seventy-nine ',
'eighty',
'eighty-one ',
'eighty-two ',
'eighty-three ',
'eighty-four ',
'eighty-five ',
'eighty-six ',
'eighty-seven ',
'eighty-eight ',
'eighty-nine ',
'ninety',
'ninety-one ',
'ninety-two ',
'ninety-three ',
'ninety-four ',
'ninety-five ',
'ninety-six ',
'ninety-seven ',
'ninety-eight ',
'ninety-nine'
)

DBFiddle Demo