Sunday, July 10, 2011
Number To Words in SQL
CREATE FUNCTION [dbo].[fn_NumToWords]
(
@Number Numeric(18,2)
)
RETURNS varchar(100) AS
BEGIN
DECLARE @M_Words TABLE (WNumber Int , Wwords VARCHAR(50))
Insert Into @M_Words(WNumber,Wwords) Values(0,'Zero')
Insert Into @M_Words(WNumber,Wwords) Values(1,'One')
Insert Into @M_Words(WNumber,Wwords) Values(2,'Two')
Insert Into @M_Words(WNumber,Wwords) Values(3,'Three')
Insert Into @M_Words(WNumber,Wwords) Values(4,'Four')
Insert Into @M_Words(WNumber,Wwords) Values(5,'Five')
Insert Into @M_Words(WNumber,Wwords) Values(6,'Six')
Insert Into @M_Words(WNumber,Wwords) Values(7,'Seven')
Insert Into @M_Words(WNumber,Wwords) Values(8,'Eight')
Insert Into @M_Words(WNumber,Wwords) Values(9,'Nine')
Insert Into @M_Words(WNumber,Wwords) Values(10,'Ten')
Insert Into @M_Words(WNumber,Wwords) Values(11,'Eleven')
Insert Into @M_Words(WNumber,Wwords) Values(12,'Twelve')
Insert Into @M_Words(WNumber,Wwords) Values(13,'Thirteen')
Insert Into @M_Words(WNumber,Wwords) Values(14,'Fourteen')
Insert Into @M_Words(WNumber,Wwords) Values(15,'Fifteen')
Insert Into @M_Words(WNumber,Wwords) Values(16,'Sixteen')
Insert Into @M_Words(WNumber,Wwords) Values(17,'Seventeen')
Insert Into @M_Words(WNumber,Wwords) Values(18,'Eighteen')
Insert Into @M_Words(WNumber,Wwords) Values(19,'Nineteen')
Insert Into @M_Words(WNumber,Wwords) Values(20,'Twenty')
Insert Into @M_Words(WNumber,Wwords) Values(30,'Thirty')
Insert Into @M_Words(WNumber,Wwords) Values(40,'Forty')
Insert Into @M_Words(WNumber,Wwords) Values(50,'Fifty')
Insert Into @M_Words(WNumber,Wwords) Values(60,'Sixty')
Insert Into @M_Words(WNumber,Wwords) Values(70,'Seventy')
Insert Into @M_Words(WNumber,Wwords) Values(80,'Eighty')
Insert Into @M_Words(WNumber,Wwords) Values(90,'Ninety')
Declare @StrNumber varchar(12),@SCrore char(2), @SLacs char(2), @SThou char(2), @SHun char(2)
Declare @STenUnt char(2), @STen char(2), @SUnt char(2), @SDecimal char(2)
Declare @ICrore Int, @ILacs Int, @IThou Int, @IHun Int, @ITenUnt Int, @ITen Int, @IUnt Int, @IDecimal Int
Declare @SNumToWords varchar(100), @Wwords varchar(10)
Select @StrNumber = Replicate('0',12-Len(LTrim(RTrim(convert(varchar,@Number))))) + LTrim(RTrim(Convert(varchar,@Number)))
Select @SNumToWords = ''
If Len(LTrim(RTrim(convert(varchar,@Number)))) > 4
Begin
--Fetch Crore
Select @SCrore = Substring(@StrNumber,1,2)
Select @ICrore = Convert(int,@SCrore)
If @ICrore > 0
Begin
Select @STen = Substring(@StrNumber,1,1)
Select @SUnt = Substring(@StrNumber,2,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,1,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ITen
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Select @SNumToWords = @SNumToWords + ' Crore'
End
Select @SLacs = Substring(@StrNumber,3,2)
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0
Begin
Select @STen = Substring(@StrNumber,3,1)
Select @SUnt = Substring(@StrNumber,4,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,3,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ITen
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Select @SNumToWords = @SNumToWords + ' Lacs'
End
Select @SThou = Substring(@StrNumber,5,2)
Select @IThou = Convert(int,@SThou)
If @IThou > 0
Begin
Select @STen = Substring(@StrNumber,5,1)
Select @SUnt = Substring(@StrNumber,6,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,5,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ITen
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Select @SNumToWords = @SNumToWords + ' Thousand '
End
Select @SHun = Substring(@StrNumber,7,1)
Select @IHun = Convert(int,@SHun)
If @IHun > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IHun
Select @SNumToWords = @SNumToWords + @Wwords + ' Hundred'
End
Select @STenUnt = Substring(@StrNumber,8,2)
---Print @STenUnt
Select @ITenUnt = Convert(int,@STenUnt)
If @ITenUnt > 0
Begin
Select @STen = Substring(@StrNumber,8,1)
Select @SUnt = Substring(@StrNumber,9,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,8,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ITen
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
End
Select @SNumToWords = @SNumToWords + Space(1) + 'Rupees' --Only/-
End
Else
Begin
Select @SLacs = Substring(LTrim(RTrim(convert(varchar,@Number))),1,1)
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0 and @ILacs <> 1
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ILacs
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords + Space(1) + 'Rupees'
End
Else
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ILacs
Select @SNumToWords = @SNumToWords + @Wwords + Space(1) + 'Rupee'
End
End
Select @SDecimal = Substring(@StrNumber,11,2)
Select @IDecimal = Convert(int,@SDecimal)
If @IDecimal > 0
Begin
Select @SNumToWords = @SNumToWords + ' and'
Select @STen = Substring(@SDecimal,1,1)
Select @SUnt = Substring(@SDecimal,2,1)
if Convert(int,@STen) = 1
Begin
Select @ITen = Convert(int,Substring(@StrNumber,11,2))
Select @IUnt = 0
End
Else
Begin
Select @ITen = Convert(int,@STen)*10
Select @IUnt = Convert(int,@SUnt)
End
If @ITen > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ITen
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Select @SNumToWords = @SNumToWords + Space(1) + 'Paise'
End
Return LTrim(RTrim(@SNumToWords))
End
GO
/*
select [dbo].[fn_NumToWords](7281)
Output : Seven Thousand Two Hundred Eighty One Rupees
*/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment