如何使用sql标量函数将数字转换为单词

时间:2015-03-02 10:24:11

标签: sql sql-server

嗨我使用以下来将数字转换为单词(字符串) 在sql server标量函数中但没有获得所需的结果。

    GO
/****** Object:  UserDefinedFunction [dbo].[fNumToWords]    Script Date: 03/02/2015 15:44:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[fNumToWords]
(@decNumber decimal(12, 2)) 
returns varchar(300) 
As 
Begin 
Declare 
 @strnum varchar(100), 
 @strCents varchar(100), 
 @strWords varchar(300), 
 @intIndex integer 


Select @strnum = Cast(@decNumber as varchar(100)) 
Select @intIndex = CharIndex('.', @strnum) 
select @strCents = '' 

if(@decNumber>999999999.99) 
BEGIN  
 RETURN '' 
END 

If @intIndex > 0  
begin 
 Select @strCents = dbo.fConvertTens(Right(@strnum, Len(@strnum) - @intIndex)) 
 Select @strnum = SubString(@strnum, 1, Len(@strnum) - 3) 
 If Len(@strCents) > 0 Select @strCents = @strCents + ' Cents' 
end 

declare @trail_zeros  varchar(3) 
declare @strthousands varchar(3) 
declare @strMillions varchar(3) 

set @trail_zeros = '000' 

if len(@strnum) <= 3 
begin 
 select @strWords = dbo.fConvertHundreds(left(@trail_zeros,3-len(right(@strnum,3)))+ right(@strnum,3)) 
end  
if len(@strnum) >= 4 and len(@strnum) <=6 
begin 
 select @strthousands = left(@trail_zeros,3 - len(left(right(@strnum,6),len(@strnum)-3))) + left(right(@strnum,6),len(@strnum)-3) 
 select @strWords = dbo.fConvertHundreds(@strthousands) + ' Thousand ' + dbo.fConvertHundreds(left(@trail_zeros,3-len(right(@strnum,3)))+ right(@strnum,3)) 
end 
if len(@strnum) >= 7 and len(@strnum) <=9 
begin 
 select @strMillions = left(@trail_zeros,3-len(left(@strnum,len(@strnum)-6))) + left(@strnum,len(@strnum)-6) 
 select @strthousands = left(right(@strnum,6),3) 
 select @strWords = dbo.fConvertHundreds(@strMillions) + ' Lakhs ' + dbo.fConvertHundreds(@strthousands) + ' Thousand ' + dbo.fConvertHundreds(left(@trail_zeros,3-len(right(@strnum,3)))+ right(@strnum,3)) 
end 

if @strCents <> '' 
 select @strWords = @strWords + ' and ' + @strCents + ' Only' 
else 
 select @strWords = @strWords + ' Only' 

return  @strWords 

end 

它将显示输出

只有六十六万四千六百五十

但我想要 六十六万六十四数千五百五十 fconverthunreds ....

> `
/****** Object:  UserDefinedFunction [dbo].[fConvertHundreds]    Script Date: 03/02/2015 18:57:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Function [dbo].[fConvertHundreds] (@decNumber varchar(3)) 
returns varchar(200) 
as  
Begin 
declare @strWords varchar(200) 

 Select @strWords = Case left(@decNumber,1) 
     When '1' then 'One' 
     When '2' then 'Two' 
     When '3' then 'Three' 
     When '4' then 'Four' 
     When '5' then 'Five' 
     When '6' then 'Six'  
     When '7' then 'Seven' 
     When '8' then 'Eight' 
     When '9' then 'Nine' 
     Else '' 
 end 

 if ltrim(rtrim(@strWords)) <> '' and @strWords is not null 
  select @strWords = @strWords + ' Hundred '+ dbo.fconvertTens(right(@decNumber,2)) 
 else 
  select @strWords = dbo.fconvertTens(right(@decNumber,2)) 

return @strWords 
end `

///// 10 ...

 ALTER    Function [dbo].[fConvertTens](@decNumber varchar(2)) 
returns varchar(30) 
as 
Begin 
declare 
@strWords varchar(30) 
-- If amt is between 10 and 19
If Left(@decNumber, 1) = 1  
begin 
 Select @strWords = Case @decNumber 
     When '10' then 'Ten' 
     When '11' then 'Eleven' 
     When '12' then 'Twelve' 
     When '13' then 'Thirteen' 
     When '14' then 'Fourteen' 
     When '15' then 'Fifteen' 
     When '16' then 'Sixteen' 
     When '17' then 'Seventeen' 
     When '18' then 'Eighteen' 
     When '19' then 'Nineteen' 
 end 
end 
else  -- if amt is between 20 and 99
begin 
 Select @strWords = Case Left(@decNumber, 1) 
     When '0' then ''   
     When '2' then 'Twenty ' 
     When '3' then 'Thirty ' 
     When '4' then 'Forty ' 
     When '5' then 'Fifty ' 
     When '6' then 'Sixty ' 
     When '7' then 'Seventy ' 
     When '8' then 'Eighty ' 
     When '9' then 'Ninety ' 
 end 
 Select @strWords = @strWords + dbo.fConvertDigit(Right(@decNumber, 1)) 
end 
 --Convert ones place digit. 

return @strWords 
end 

1 个答案:

答案 0 :(得分:1)

请检查此链接。

http://dotnet-assembly.blogspot.in/2012/08/sql-server-query-to-convert-numbers.html

select dbo.[NumberToWords](664650)  --RUN THIS AFTER ABOVE LINK'S FUNCTION EXECUTE

而且,我在下面的链接中更改了一部分。

http://www.sqlservercentral.com/Forums/Topic794134-149-1.aspx

select dbo.fnNumberToWords(664650) --RUN THIS AFTER BELOW ALL FUNCTION EXECUTE

--1. function
CREATE FUNCTION [dbo].[fnBelow100] 
(
@Num int
)

RETURNS varchar(50)

AS

BEGIN

Declare @Name varchar(25)

if(@Num=2) set @Name='Twenty'
if(@Num=3) set @Name='Thirty'
if(@Num=4) set @Name='Fourty'
if(@Num=5) set @Name='Fifty'
if(@Num=6) set @Name='Sixty'
if(@Num=7) set @Name='Seventy'
if(@Num=8) set @Name='Eighty'
if(@Num=9) set @Name='Ninety'
return @Name

END


--2nd function 

CREATE FUNCTION [dbo].[fnBelow20] 
(
@Num int
)

RETURNS varchar(50)

AS

BEGIN

Declare @Name varchar(25)


if(@Num=1) set @Name='One'
if(@Num=2) set @Name='Two'
if(@Num=3) set @Name='Three'
if(@Num=4) set @Name='Four'
if(@Num=5) set @Name='Five'
if(@Num=6) set @Name='Six'
if(@Num=7) set @Name='Seven'
if(@Num=8) set @Name='Eight'
if(@Num=9) set @Name='Nine'
if(@Num=10) set @Name='Ten'
if(@Num=11) set @Name='Eleven'
if(@Num=12) set @Name='Twelve'
if(@Num=13) set @Name='Thirteen'
if(@Num=14) set @Name='Forteen'
if(@Num=15) set @Name='Fifteen'
if(@Num=16) set @Name='Sixteen'
if(@Num=17) set @Name='Seventeen'
if(@Num=18) set @Name='Eighteen'
if(@Num=19) set @Name='Nineteen'
return @Name
END

--3. RECURSIVE FUNCTION WHICH GIVE ACTUAL OUTPUT
USE [tempdb]
GO
/****** Object:  UserDefinedFunction [dbo].[NumberToWords]    Script Date: 3/2/2015 6:04:17 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[NumberToWords] 
(
@Number bigint
)

RETURNS varchar(1024)

AS
BEGIN

DECLARE @InwWords varchar(1024)

set @InwWords =
(
SELECT Case
WHEN @Number BETWEEN 1 AND 19
THEN (SELECT dbo.fnBelow20(@Number))
WHEN @Number BETWEEN 20 AND 99 
THEN (SELECT dbo.fnBelow100(@Number/10))+ '-' + dbo.NumberToWords( @Number % 10)
WHEN @Number BETWEEN 100 AND 999 
THEN (dbo.NumberToWords( @Number / 100))+' Hundred '+ dbo.NumberToWords( @Number % 100)
WHEN @Number BETWEEN 1000 AND 99999 
THEN (dbo.NumberToWords( @Number / 1000))+' Thousand '+ dbo.NumberToWords( @Number % 1000) 
WHEN @Number BETWEEN 100000 AND 9999999 
THEN (dbo.NumberToWords( @Number / 100000))+' Lac '+ dbo.NumberToWords( @Number % 100000)
WHEN @Number >= 10000000 
THEN (dbo.NumberToWords( @Number / 10000000))+' Crore '+ dbo.NumberToWords( @Number % 10000000)
WHEN @Number = 0
then ''
ELSE ' INVALID INPUT' END
)
SELECT @InwWords = RTRIM(@InwWords)
SELECT @InwWords = RTRIM(LEFT(@InwWords,len(@InwWords)-1)) WHERE RIGHT(@InwWords,1)='-'
return (@InwWords)
END