TSQL - 将资金转换为西班牙文本

时间:2018-02-21 15:26:43

标签: tsql

我在另一个来源中遇到了一个很酷的功能,它将货币数据类型转换为英文文本。这很棒,但我还需要用西班牙语这样做。我试图将数字编辑为西班牙语单词,但当然西班牙语编号的“规则”与英语不同。有没有人已经将货币数据类型转换为西班牙文本?以下是英文版供参考。或者讲西班牙语的数据库开发人员可以帮我修改这个吗?

CREATE FUNCTION [dbo].[fnNumberToEnglish](@Money AS money) 
RETURNS VARCHAR(1024)   
AS

BEGIN

DECLARE @Number as BIGINT

SET @Number = FLOOR(@Money)

DECLARE @Below20 TABLE (ID int identity(0,1), Word varchar(32))

DECLARE @Below100 TABLE (ID int identity(2,1), Word varchar(32))

INSERT @Below20 (Word) VALUES 

          ( 'Zero'), ('One'),( 'Two' ), ( 'Three'),

          ( 'Four' ), ( 'Five' ), ( 'Six' ), ( 'Seven' ),

          ( 'Eight'), ( 'Nine'), ( 'Ten'), ( 'Eleven' ),

          ( 'Twelve' ), ( 'Thirteen' ), ( 'Fourteen'),

          ( 'Fifteen' ), ('Sixteen' ), ( 'Seventeen'),

          ('Eighteen' ), ( 'Nineteen' ) 

 INSERT @Below100 VALUES ('Twenty'), ('Thirty'),('Forty'), ('Fifty'),

             ('Sixty'), ('Seventy'), ('Eighty'), ('Ninety')

DECLARE @English varchar(1024) = 
(
  SELECT Case 
  WHEN @Number = 0 THEN  ''
  WHEN @Number BETWEEN 1 AND 19 
  THEN (SELECT Word FROM @Below20 WHERE ID=@Number)
  WHEN @Number BETWEEN 20 AND 99
  -- SQL Server recursive function    

  THEN  (SELECT Word FROM @Below100 WHERE ID=@Number/10)+ '-' +
   dbo.fnMoneyToEnglish( @Number % 10) 
   WHEN @Number BETWEEN 100 AND 999   
   THEN  (dbo.fnMoneyToEnglish( @Number / 100))+' Hundred '+

 dbo.fnMoneyToEnglish( @Number % 100) 

 WHEN @Number BETWEEN 1000 AND 999999   
 THEN  (dbo.fnMoneyToEnglish( @Number / 1000))+' Thousand '+
 dbo.fnMoneyToEnglish( @Number % 1000)  
 WHEN @Number BETWEEN 1000000 AND 999999999   
 THEN  (dbo.fnMoneyToEnglish( @Number / 1000000))+' Million '+
 dbo.fnMoneyToEnglish( @Number % 1000000) 

 ELSE ' INVALID INPUT' END

)

SELECT @English = RTRIM(@English)

SELECT @English = RTRIM(LEFT(@English,len(@English)-1))
WHERE RIGHT(@English,1)='-'

IF @@NestLevel = 1
BEGIN

SELECT @English = @English+' POINT '

SELECT @English = @English+ 

convert(varchar,convert(int,100*(@Money - @Number))) 

END

RETURN (@English)

END

1 个答案:

答案 0 :(得分:0)

所以我刚刚学会了西班牙语并自己做了。享受!

ALTER FUNCTION [dbo].[fnMoneyToSpanish](@Money AS money) 

RETURNS VARCHAR(1024)

AS

BEGIN

DECLARE @Number as BIGINT

SET @Number = FLOOR(@Money)

DECLARE @Below20 TABLE (ID int identity(0,1), Word varchar(32))

DECLARE @Below100 TABLE (ID int identity(2,1), Word varchar(32))

INSERT @Below20 (Word) VALUES 

          ( 'cero'), ('uno'),( 'dos' ), ( 'tres'),

          ( 'cuatro' ), ( 'cinco' ), ( 'seis' ), ( 'siete' ),

          ( 'ocho'), ( 'nueve'), ( 'diez'), ( 'once' ),

          ( 'doce' ), ( 'trece' ), ( 'catorce'),

          ( 'quince' ), ('dieciséis' ), ( 'diecisiete'),

          ('dieciocho' ), ( 'diecinueve' ) 

 INSERT @Below100 VALUES ('veinti'), ('treinta'),('cuarenta,'), ('cincuenta'),

             ('sesenta'), ('setenta'), ('ochenta'), ('coventa')

DECLARE @English varchar(1024) = 

(

  SELECT Case 

  WHEN @Number = 0 THEN  ''

  WHEN @Number BETWEEN 1 AND 19 

  THEN (SELECT Word FROM @Below20 WHERE ID=@Number)

  WHEN @Number BETWEEN 20 AND 99 

  THEN (SELECT CASE WHEN WORD = 'veinti' AND @Number = '20' THEN 'viente' ELSE WORD END FROM(SELECT Word FROM @Below100 WHERE ID=@Number/10)d) + CASE WHEN @@NestLevel in (3,4) AND (SELECT Word FROM @Below100 WHERE ID=@Number/10) <> 'veinti' THEN ' y ' ELSE '' END + --concat(' Number:',@Number,' Level:', @@NestLevel, ' ') +

   dbo.fnMoneyToSpanish( @Number % 10) 

   WHEN @Number BETWEEN 100 AND 999   

   THEN CASE WHEN @Number < 200 THEN ' ciento ' ELSE (dbo.fnMoneyToSpanish( @Number / 100)) + 'cientos ' END +

 dbo.fnMoneyToSpanish( @Number % 100) 

   WHEN @Number BETWEEN 1000 AND 999999   

  THEN CASE WHEN @Number < 2000 THEN ' mil ' ELSE  (dbo.fnMoneyToSpanish( @Number / 1000)) + ' mil ' END +

 dbo.fnMoneyToSpanish( @Number % 1000)  

 WHEN @Number BETWEEN 1000000 AND 999999999   

 THEN CASE WHEN @Number < 200000 THEN ' millón ' ELSE  (dbo.fnMoneyToSpanish( @Number / 1000000)) + ' millones ' END +

 dbo.fnMoneyToSpanish( @Number % 1000000) 

  ELSE ' INVALID INPUT' END

)

SELECT @English = RTRIM(@English)

SELECT @English = RTRIM(LEFT(@English,len(@English)-1))

     WHERE RIGHT(@English,1)=' y '

IF @@NestLevel = 1

BEGIN

SELECT @English = @English+' dólares y '

SELECT @English = @English+ 

convert(varchar,convert(int,100*(@Money - @Number))) +' cents'

END

RETURN (ltrim(@English))

END

--select [dbo].[fnMoneyToSpanish](2654876.36)