create FUNCTION [dbo].[fn_UnitWiseAmountIntoWords]
(
@Number Numeric(18,2)
)
RETURNS @L_FinalTable Table
( Crore Varchar(50),
Lacs Varchar(50),
Thousand Varchar(50),
Hundred Varchar(50),
Ten Varchar(50),
Unit Varchar(50)
)
As
Begin
Insert Into @L_FinalTable(Crore,Lacs,Thousand,Hundred,Ten,Unit) Values('','','','','','')
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)))
--Print @StrNumber
--Print Len(@StrNumber)
Select @SNumToWords = ''
--Print Len(LTrim(RTrim(convert(varchar,@Number))))
If Len(LTrim(RTrim(convert(varchar,@Number)))) > 4
Begin
--Fetch Crore
Set @SNumToWords = ''
--Print Len(@StrNumber)
Select @SCrore = Substring(@StrNumber,1,2)
--Print @SCrore
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Crore = @SNumToWords
End
--Print Len(@StrNumber)
Select @SLacs = Substring(@StrNumber,3,2)
--Print @SLacs
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0
Begin
Select @STen = Substring(@StrNumber,3,1)
Select @SUnt = Substring(@StrNumber,4,1)
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Lacs = @SNumToWords
End
Select @SThou = Substring(@StrNumber,5,2)
--Print @SThou
Select @IThou = Convert(int,@SThou)
If @IThou > 0
Begin
Select @STen = Substring(@StrNumber,5,1)
Select @SUnt = Substring(@StrNumber,6,1)
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Thousand = @SNumToWords
End
Select @SHun = Substring(@StrNumber,7,1)
--Print @SHun
Select @IHun = Convert(int,@SHun)
If @IHun > 0
Begin
Set @SNumToWords = ''
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IHun
--Print @Wwords
Update @L_FinalTable Set Hundred = @Wwords
End
Select @STenUnt = Substring(@StrNumber,8,2)
---Print @STenUnt
Select @ITenUnt = Convert(int,@STenUnt)
If @ITenUnt > 0
Begin
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Ten = @SNumToWords
End
End
Else
Begin
--Print Len(@StrNumber)
--Print LTrim(RTrim(convert(varchar,@Number)))
Select @SLacs = Substring(LTrim(RTrim(convert(varchar,@Number))),1,1)
--Print @SLacs
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0 and @ILacs <> 1
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords -- + Space(1) + 'Rupees'
End
Else
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + @Wwords --+ Space(1) + 'Rupee'
End
Update @L_FinalTable Set Ten = @Wwords
End
Select @SDecimal = Substring(@StrNumber,11,2)
Select @IDecimal = Convert(int,@SDecimal)
If @IDecimal > 0
Begin
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Unit = @SNumToWords
End
Return
End
/*
select * from [dbo].fn_UnitWiseAmountIntoWords(11057281.2808)
Output :
Crore Lacs Thousand Hundred Ten Unit
One Ten Fifty Seven Two Eighty One Twenty Eight
*/
(
@Number Numeric(18,2)
)
RETURNS @L_FinalTable Table
( Crore Varchar(50),
Lacs Varchar(50),
Thousand Varchar(50),
Hundred Varchar(50),
Ten Varchar(50),
Unit Varchar(50)
)
As
Begin
Insert Into @L_FinalTable(Crore,Lacs,Thousand,Hundred,Ten,Unit) Values('','','','','','')
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)))
--Print @StrNumber
--Print Len(@StrNumber)
Select @SNumToWords = ''
--Print Len(LTrim(RTrim(convert(varchar,@Number))))
If Len(LTrim(RTrim(convert(varchar,@Number)))) > 4
Begin
--Fetch Crore
Set @SNumToWords = ''
--Print Len(@StrNumber)
Select @SCrore = Substring(@StrNumber,1,2)
--Print @SCrore
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Crore = @SNumToWords
End
--Print Len(@StrNumber)
Select @SLacs = Substring(@StrNumber,3,2)
--Print @SLacs
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0
Begin
Select @STen = Substring(@StrNumber,3,1)
Select @SUnt = Substring(@StrNumber,4,1)
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Lacs = @SNumToWords
End
Select @SThou = Substring(@StrNumber,5,2)
--Print @SThou
Select @IThou = Convert(int,@SThou)
If @IThou > 0
Begin
Select @STen = Substring(@StrNumber,5,1)
Select @SUnt = Substring(@StrNumber,6,1)
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Thousand = @SNumToWords
End
Select @SHun = Substring(@StrNumber,7,1)
--Print @SHun
Select @IHun = Convert(int,@SHun)
If @IHun > 0
Begin
Set @SNumToWords = ''
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IHun
--Print @Wwords
Update @L_FinalTable Set Hundred = @Wwords
End
Select @STenUnt = Substring(@StrNumber,8,2)
---Print @STenUnt
Select @ITenUnt = Convert(int,@STenUnt)
If @ITenUnt > 0
Begin
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Ten = @SNumToWords
End
End
Else
Begin
--Print Len(@StrNumber)
--Print LTrim(RTrim(convert(varchar,@Number)))
Select @SLacs = Substring(LTrim(RTrim(convert(varchar,@Number))),1,1)
--Print @SLacs
Select @ILacs = Convert(int,@SLacs)
If @ILacs > 0 and @ILacs <> 1
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords -- + Space(1) + 'Rupees'
End
Else
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @ILacs
--Print @Wwords
Select @SNumToWords = @SNumToWords + @Wwords --+ Space(1) + 'Rupee'
End
Update @L_FinalTable Set Ten = @Wwords
End
Select @SDecimal = Substring(@StrNumber,11,2)
Select @IDecimal = Convert(int,@SDecimal)
If @IDecimal > 0
Begin
Set @SNumToWords = ''
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
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Tens'
End
If @IUnt > 0
Begin
Select @Wwords = ''
Select @Wwords = Wwords From @M_Words Where WNumber = @IUnt
--Print @Wwords
Select @SNumToWords = @SNumToWords + Space(1) + @Wwords --' Unit'
End
Update @L_FinalTable Set Unit = @SNumToWords
End
Return
End
/*
select * from [dbo].fn_UnitWiseAmountIntoWords(11057281.2808)
Output :
Crore Lacs Thousand Hundred Ten Unit
One Ten Fifty Seven Two Eighty One Twenty Eight
*/
No comments:
Post a Comment