sql server中是不是有可以将数字转换为单词的内置函数

Posted

技术标签:

【中文标题】sql server中是不是有可以将数字转换为单词的内置函数【英文标题】:Is there a built-in function that can convert numbers into words in sql serversql server中是否有可以将数字转换为单词的内置函数 【发布时间】:2016-10-20 03:50:03 【问题描述】:

我有一张桌子currency

Dollar  
------  
10   
100  
1000    
120.20  

O/P

Dollar  
--------    
Ten dollar  
Hundred dollar  
One Thousand dollar  
One Hundred Twenty dollar and Twenty cents

【问题讨论】:

那么我们有什么方法可以实现吗?? 查看此链接forums.asp.net/t/… 这是在 SO ***.com/questions/1673265/…中提出的类似问题 【参考方案1】:

在我编写的SQL# SQLCLR 库的免费版本中有一个函数 Util_ToWords 可以做到这一点(嗯,没有“美元”和“美分”字样添加):

SELECT SQL#.Util_ToWords(10); -- Ten
SELECT SQL#.Util_ToWords(100); -- One Hundred
SELECT SQL#.Util_ToWords(1000); -- One Thousand 
SELECT SQL#.Util_ToWords(120.20); -- One Hundred Twenty and 20
SELECT SQL#.Util_ToWords(212); -- Two Hundred Twelve
SELECT SQL#.Util_ToWords(123097.4);-- One Hundred Twenty Three Thousand, Ninety Seven and 40

以下示例将“dollar(s)”和“cents”字样注入返回值(最初用于打印支票):

DECLARE @Amount MONEY = 2.08;

;WITH cte AS
(
  SELECT N' dollar' + CASE WHEN @Amount >= 1.00 AND @Amount < 2.00 THEN N''
              ELSE N's'
         END AS [Currency],
         SQL#.Util_ToWords(@Amount) AS [Words]
)
SELECT CASE CHARINDEX(N' and ', cte.[Words])
           WHEN 0 THEN cte.[Words] + cte.[Currency]
           ELSE STUFF(cte.[Words], CHARINDEX(N' and ', cte.[Words]), 0, cte.[Currency])
                + N' cents'
       END
FROM cte;

返回:

Two dollars and 08 cents

【讨论】:

【参考方案2】:

它没有内置语法。您必须在您的应用程序上执行此操作或创建一个函数。然后将转换后的金额存储到 SQL 数据库中。

【讨论】:

【参考方案3】:

首先创建一个函数

Create    FUNCTION [dbo].[OneToNinetynine] (

@num int)   RETURNS VARCHAR(50) 

AS BEGIN
DECLARE @word VARCHAR(50)

set @word=''

 if @num =1 set @word='One'

 else if @num =2 set @word='Two'

 else if @num = 3    set @word='Three'

 else if @num = 4    set @word='Four'

else if @num =  5    set @word='Five'

else if @num =  6    set @word='Six'

else if @num =  7    set @word='Seven'

else if @num =  8    set @word='Eight'

else if @num =  9    set @word='Nine'

else if @num =  10   set @word='Ten'

else if @num =  11   set @word='Eleven'

else if @num =  12   set @word='Twelve'

else if @num =  13   set @word='Thirteen'

 else if @num = 14   set @word='Forteen'

 else if @num = 15   set @word='Fifteen'

 else if @num = 16   set @word='Sixteen'

else if @num =  17   set @word='Seventeen'

else if @num =  18   set @word='Eighteen'

else if @num =  19   set @word='Nineteen'

else if @num =  20   set @word='Twenty'

else if @num =  21   set @word='Twenty One '

else if @num =  22   set @word='Twenty Two'

else if @num =  23   set @word='Twenty Three'

else if @num =  24   set @word='Twenty Four'

else if @num =  25   set @word='Twenty Five'

else if @num =  26   set @word='Twenty Six'

else if @num =  27   set @word='Twenty Seven'

else if @num =  28   set @word='Twenty Eight'

else if @num =  29   set @word='Twenty Nine'

else if @num =  30   set @word='Thirty'

else if @num =  31   set @word='Thirty One '

else if @num =  32   set @word='Thirty Two'

else if @num =  33   set @word='Thirty Three'

else if @num =  34   set @word='Thirty Four'

else if @num =  35   set @word='Thirty Five'

else if @num =  36   set @word='Thirty Six'

else if @num =  37   set @word='Thirty Seven'

else if @num =  38   set @word='Thirty Eight'

else if @num =  39   set @word='Thirty Nine'

else if @num =  40   set @word='Forty'

else if @num =  41   set @word='Forty One'

else if @num =  42   set @word='Forty Two'

else if @num =  43   set @word='Forty Three'

else if @num =  44   set @word='Forty Four'

else if @num =  45   set @word='Forty Five'

else if @num =  46   set @word='Forty Six'

else if @num =  47   set @word='Forty Seven'

else if @num =  48   set @word='Forty Eight'

else if @num =  49   set @word='Forty Nine'

else if @num =  50   set @word='Fifty'

else if @num =  51   set @word='Fifty One'

else if @num =  52   set @word='Fifty Two'

else if @num =  53   set @word='Fifty Three'

else if @num =  54   set @word='Fifty Four'

else if @num =  55   set @word='Fifty Five'

else if @num =  56   set @word='Fifty Six'

 else if @num = 57   set @word='Fifty Seven'

else if @num =  58   set @word='Fifty Eight'

 else if @num = 59   set @word='Fifty Nine'

else if @num =  60   set @word='Sixty'

 else if @num = 61   set @word='Sixty One'

 else if @num = 62   set @word='Sixty Two'

else if @num =  63   set @word='Sixty Three'

else if @num =  64   set @word='Sixty Four'

 else if @num = 65   set @word='Sixty Five'

 else if @num = 66   set @word='Sixty Six'

 else if @num = 67   set @word='Sixty Seven'

 else if @num = 68   set @word='Sixty Eight'

 else if @num = 69   set @word='Sixty Nine'

else if @num =  70   set @word='Seventy'

 else if @num = 71   set @word='Seventy One '

 else if @num = 72   set @word='Seventy Two'

 else if @num = 73   set @word='Seventy Three'

 else if @num = 74   set @word='Seventy Four'

 else if @num = 75   set @word='Seventy Five'

 else if @num = 76   set @word='Seventy Six'

然后使用这个函数

     Create    FUNCTION [dbo].[NumberToWord] (  

     @num numeric(16,2)  

 )   RETURNS VARCHAR(200)  

 AS BEGIN  


 DECLARE @word VARCHAR(200)  

 DECLARE @Dollar VARCHAR(200)  

 DECLARE @Cent VARCHAR(200)

 DECLARE @main int  

 DECLARE @Frac numeric(12,2)

 DECLARE @i int  

 set @word ='' 

 set @Dollar =''

 set @Cent=''

 IF(@num<1000000000)

 BEGIN


 set @main = convert(int,@num)  

 set @Frac = (@num - @main  )*100





 IF (@main>9999999)  

 Begin  

  set @Dollar=@Dollar+dbo.OneToNinetynine(@main/10000000)+'Crore ' 

  set @main= @main % 10000000  

 End  

 IF (@main>99999)

 Begin  

  set @Dollar=@Dollar+dbo.OneToNinetynine(@main/100000)+'Lac '  

  set @main= @main % 100000  

 End  

 IF (@main>999)  

 Begin

  set @Dollar=@Dollar+dbo.OneToNinetynine(@main/1000)+'Thousand '  

  set @main= @main % 1000  

 End  

 IF (@main>99)   

 Begin  

  set @Dollar=@Dollar+dbo.OneToNinetynine(@main/100)+'Hundred '  

  set @main= @main % 100 

 End  


 IF (@main>0)  

 Begin  

  set @Dollar=@Dollar+dbo.OneToNinetynine(@main)

 End  


 IF (@Frac>0)  

 Begin 

  set @Cent=dbo.OneToNinetynine(@Frac)  

 End  


 set @word = @Dollar

 if @Dollar <> '' set @word = @word + 'Dollar'

 if (@Dollar <> '' and @Cent <> '') set @word =@word +  ' And '

 if @Cent <> '' set @word =@word  +  @Cent + 'Paisa'

 set @word ='Tk-'+ @word + ' Only' 

 END   


 RETURN (@word)  
 END  

然后调用第二个函数 选择 dbo.NumberToWord(24450999.89)

【讨论】:

以上是关于sql server中是不是有可以将数字转换为单词的内置函数的主要内容,如果未能解决你的问题,请参考以下文章

sql server中的怎么把数值型转换为字符串

从 SQL Server 2005 中的用户定义函数中“捕获”错误

在 SQL 中将数字转换为单词 [重复]

SQL Server 2012 将数据类型 varchar 转换为数字时出错

sql server2000如何将数字转换为日期时间

切换语句将单词转换为数字? C ++