-- drop FUNCTION InWord
-- go

CREATE FUNCTION InWord
(@num numeric)
RETURNS varchar(500)
AS

    BEGIN
    DECLARE @cNum numeric
    SET @cNum = @num

    DECLARE @tblNum TABLE
    (Num int, NumStr varchar(20))
    INSERT INTO @tblNum
    SELECT 1, ' GK' UNION SELECT 2, ' `yB' UNION    SELECT 3, ' wZb' UNION SELECT 4, ' Pvi' UNION
    SELECT 5, ' cuvP' UNION SELECT 6, ' Qq' UNION    SELECT 7, ' mvZ' UNION SELECT 8, ' AvU' UNION
    SELECT 9, ' bq' UNION SELECT 10, ' `k' UNION    SELECT 11, ' GMvi' UNION SELECT 12, ' evi' UNION
    SELECT 13, ' †Zi' UNION SELECT 14, ' †PŠÏ' UNION    SELECT 15, ' c‡bi' UNION SELECT 16, ' †lvj' UNION
    SELECT 17, ' m‡Zi' UNION SELECT 18, ' AvVvi' UNION    SELECT 19, ' Ewbk' UNION SELECT 20, ' wek' UNION
    SELECT 21, ' GKzk' UNION SELECT 22, ' evBk' UNION    SELECT 23, ' †ZBk' UNION SELECT 24, ' PweŸk' UNION
    SELECT 25, ' cuwPk' UNION SELECT 26, ' QvweŸk' UNION    SELECT 27, ' mvZvk' UNION SELECT 28, ' AvVvk' UNION
    SELECT 29, ' EbwÎk' UNION SELECT 30, ' wÎk' UNION    SELECT 31, ' GKwÎk' UNION SELECT 32, ' ewÎk' UNION
    SELECT 33, ' †ZwÎk' UNION SELECT 34, ' †PŠwÎk' UNION    SELECT 35, ' cqwÎk' UNION SELECT 36, ' QwÎk' UNION
    SELECT 37, ' mvBwÎk' UNION SELECT 38, ' AvUwÎk' UNION    SELECT 39, ' DbPwj­k' UNION SELECT 40, ' Pwj­k' UNION
    SELECT 41, ' GKPwj­k' UNION SELECT 42, ' weqvwj­k' UNION    SELECT 43, ' †ZZvwj­k' UNION SELECT 44, ' Pzqvwj­k' UNION
    SELECT 45, ' cqZvwj­k' UNION SELECT 46, ' †QPwj­k' UNION    SELECT 47, ' mvZPwj­k' UNION SELECT 48, ' AvUPwj­k' UNION
    SELECT 49, ' EbcÂvk' UNION SELECT 50, ' cÂvk' UNION    SELECT 51, ' GKvbœ' UNION SELECT 52, ' evqvbœ' UNION
    SELECT 53, ' wZàvbœ' UNION SELECT 54, ' Pzqvbœ' UNION    SELECT 55, ' cÂvbœ' UNION SELECT 56, ' Qvàvbœ' UNION
    SELECT 57, ' mvZvbœ' UNION SELECT 58, ' AvUvbœ' UNION    SELECT 59, ' DblvU' UNION SELECT 60, ' lvU' UNION
    SELECT 61, ' GKlwÆ' UNION SELECT 62, ' evlwÆ' UNION    SELECT 63, ' †ZlwÆ' UNION SELECT 64, ' †PŠlwÆ' UNION
    SELECT 65, ' cqlwÆ' UNION SELECT 66, ' †QlwÆ' UNION    SELECT 67, ' mvZlwÆ' UNION SELECT 68, ' AvUlwÆ' UNION
    SELECT 69, ' DbmËi' UNION SELECT 70, ' mËi' UNION    SELECT 71, ' GKvËi' UNION SELECT 72, ' evnvËi' UNION
    SELECT 73, ' †ZnvËi' UNION SELECT 74, ' PzqvËi' UNION    SELECT 75, ' cPvËi' UNION SELECT 76, ' wQqvËi' UNION
    SELECT 77, ' mvZvËi' UNION SELECT 78, ' AvUvËi' UNION    SELECT 79, ' DbAvwk' UNION SELECT 80, ' Avwk' UNION
    SELECT 81, ' GKvwk' UNION SELECT 82, ' weivwk' UNION    SELECT 83, ' wZivwk' UNION SELECT 84, ' Pzivwk' UNION
    SELECT 85, ' cuPvwk' UNION SELECT 86, ' wQqvwk' UNION    SELECT 87, ' mvZvwk' UNION SELECT 88, ' AvUvwk' UNION
    SELECT 89, ' DbbeŸB' UNION SELECT 90, ' beŸB' UNION    SELECT 91, ' GKvbeŸB' UNION SELECT 92, ' weivbeŸB' UNION
    SELECT 93, ' wZivbeŸB' UNION SELECT 94, ' PzivbeŸB' UNION    SELECT 95, ' cuPvbeŸB' UNION SELECT 96, ' wQqvbeŸB' UNION
    SELECT 97, ' mvZvbeŸB' UNION SELECT 98, ' AvUvbeŸB' UNION    SELECT 99, ' wbivbeŸB'

Declare @Crore	integer
Declare @Lac	int
Declare @Thou	int
Declare @Hund	int
Declare @Unit	int
Declare @Deci	int
Declare @Word	varchar(500)
Declare @Text	varchar(50)

set @Crore =floor(@cNum/1000000000)
set @Lac = right(floor(@cNum/10000000),2)
set @Thou = right(floor(@cNum/100000),2)
set @Hund = right(floor(@cNum/10000),1)
set @Unit = right(floor(@cNum/100),2)
set @Deci = right(Floor(@cNum),2)

set @Word =''
if @Crore <> 0 
  begin
	set @Word = Master.dbo.InWord(@Crore*100)
	set @Word = substring(@Word,1,len(@Word)-5) + ' †KvwU'
  end
if @Lac <> 0 
  begin
	set @Text = ''
	select @Text =NumStr from @tblNum where Num = @Lac
	set @Word = @Word + @Text + ' jvL'
  end
if @Thou <> 0
  begin
	set @Text = ''
	select @Text =NumStr from @tblNum where Num = @Thou
	set @Word = @Word + @Text + ' nvRvi'
  end
if @Hund <> 0
  begin
	set @Text = ''
	select @Text =NumStr from @tblNum where Num = @Hund
	set @Word = @Word + @Text + ' kZ'
  end
if @Unit <> 0
  begin
	set @Text = ''
	select @Text =NumStr from @tblNum where Num = @Unit
	set @Word = @Word + @Text
  end
if ltrim(@Word) <> ''
  begin
	set @Word = @Word + ' UvKv'
  end
if @Deci <> 0
  begin
	set @Text = ''
	select @Text =NumStr from @tblNum where Num = @Deci
	set @Word = @Word + @Text + ' cqmv'
  end

RETURN @Word
END