CREATE FUNCTION dbo.Amt_In_Words
(
@Amt BIGINT,
@Paisa INT
)
RETURNS VarChar(4000)
BEGIN
DECLARE @Table1 TABLE (SlNo INT IDENTITY(1, 1), AText NVarChar(50))
DECLARE @Table2 TABLE (SlNo INT IDENTITY(1, 1), AText NVarChar(50))
INSERT INTO @Table1 Select 'One'
INSERT INTO @Table1 Select 'Two'
INSERT INTO @Table1 Select 'Three'
INSERT INTO @Table1 Select 'Four'
INSERT INTO @Table1 Select 'Five'
INSERT INTO @Table1 Select 'Six'
INSERT INTO @Table1 Select 'Seven'
INSERT INTO @Table1 Select 'Eight'
INSERT INTO @Table1 Select 'Nine'
INSERT INTO @Table1 Select 'Ten'
INSERT INTO @Table1 Select 'Eleven'
INSERT INTO @Table1 Select 'Twelve'
INSERT INTO @Table1 Select 'Thirteen'
INSERT INTO @Table1 Select 'Fourteen'
INSERT INTO @Table1 Select 'Fifteen'
INSERT INTO @Table1 Select 'Sixteen'
INSERT INTO @Table1 Select 'Seventeen'
INSERT INTO @Table1 Select 'Eighteen'
INSERT INTO @Table1 Select 'Nineteen'
INSERT INTO @Table1 Select 'Twenty'
--Now, insert the multiples
INSERT INTO @Table2 Select 'Ten'
INSERT INTO @Table2 Select 'Twenty'
INSERT INTO @Table2 Select 'Thirty'
INSERT INTO @Table2 Select 'Forty'
INSERT INTO @Table2 Select 'Fifty'
INSERT INTO @Table2 Select 'Sixty'
INSERT INTO @Table2 Select 'Seventy'
INSERT INTO @Table2 Select 'Eighty'
INSERT INTO @Table2 Select 'Ninety'
DECLARE @strWord VarChar(300)
Select @strWord = ''
DECLARE @iAmt1 BIGINT, @iAmt2 BIGINT
DECLARE @strWord1 VarChar(4000)
IF @Amt < 10000000000000 AND @Amt >= 100000000000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 100000000000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 100000000000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Kharab '
END
IF @Amt < 100000000000 AND @Amt >= 1000000000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 1000000000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 1000000000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Arab '
END
IF @Amt < 1000000000 AND @Amt >= 10000000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 10000000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 10000000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Crore '
END
IF @Amt < 10000000 AND @Amt >= 100000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 100000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 100000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Lac '
END
IF @Amt < 100000 AND @Amt >= 1000
BEGIN
SET @iAmt1 = @Amt
SET @Amt = ( @Amt % 1000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 1000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10 )
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText + ' ' FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText +' ' FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Thousand '
END
IF @Amt < 1000 AND @Amt > = 100
BEGIN
SET @iAmt1 = @Amt
SET @Amt = ( @Amt % 100 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 100
SET @strWord = ( Select @strWord + ' ' +AText + ' Hundred ' FROM @Table1 WHERE SlNo = @iAmt1)
END
IF @Amt < 100 AND @Amt > 20
BEGIN
SET @iAmt1 = @Amt
SET @Amt = ( @Amt % 10 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 10
SET @strWord = ( Select @strWord + AText + ' ' FROM @Table2 WHERE SlNo = @iAmt1 )
END
IF @Amt <= 20 AND @Amt >= 1
BEGIN
SET @strWord = ( Select @strWord + AText +' ' FROM @Table1 WHERE SlNo = @Amt )
END
DECLARE @strWordP VarChar(300)
SET @strWordP = ''
IF @Paisa <> 0 BEGIN
IF @Paisa < 100 AND @Paisa > 20
BEGIN
DECLARE @Paisa_01 VarChar(300)
SET @Paisa_01 = @Paisa
SET @Paisa = ( @Paisa % 10 )
SET @Paisa_01 = ( @Paisa_01 - @Paisa ) / 10
SET @strWordP = ( Select @strWordP + AText FROM @Table2 WHERE SlNo = @Paisa_01 )
END
IF @Paisa <= 20 AND @Paisa >= 1
BEGIN
SET @strWordP = ( Select @strWordP + AText FROM @Table1 WHERE SlNo = @Paisa )
END
SET @strWord = @strWord + 'And ' + @strWordP + ' Paisa'
END
RETURN (REPLACE(@strWord, ' ', ' '))
END
GO
--Example
Select dbo.Amt_In_Words(6543211235412, 55)
(
@Amt BIGINT,
@Paisa INT
)
RETURNS VarChar(4000)
BEGIN
DECLARE @Table1 TABLE (SlNo INT IDENTITY(1, 1), AText NVarChar(50))
DECLARE @Table2 TABLE (SlNo INT IDENTITY(1, 1), AText NVarChar(50))
INSERT INTO @Table1 Select 'One'
INSERT INTO @Table1 Select 'Two'
INSERT INTO @Table1 Select 'Three'
INSERT INTO @Table1 Select 'Four'
INSERT INTO @Table1 Select 'Five'
INSERT INTO @Table1 Select 'Six'
INSERT INTO @Table1 Select 'Seven'
INSERT INTO @Table1 Select 'Eight'
INSERT INTO @Table1 Select 'Nine'
INSERT INTO @Table1 Select 'Ten'
INSERT INTO @Table1 Select 'Eleven'
INSERT INTO @Table1 Select 'Twelve'
INSERT INTO @Table1 Select 'Thirteen'
INSERT INTO @Table1 Select 'Fourteen'
INSERT INTO @Table1 Select 'Fifteen'
INSERT INTO @Table1 Select 'Sixteen'
INSERT INTO @Table1 Select 'Seventeen'
INSERT INTO @Table1 Select 'Eighteen'
INSERT INTO @Table1 Select 'Nineteen'
INSERT INTO @Table1 Select 'Twenty'
--Now, insert the multiples
INSERT INTO @Table2 Select 'Ten'
INSERT INTO @Table2 Select 'Twenty'
INSERT INTO @Table2 Select 'Thirty'
INSERT INTO @Table2 Select 'Forty'
INSERT INTO @Table2 Select 'Fifty'
INSERT INTO @Table2 Select 'Sixty'
INSERT INTO @Table2 Select 'Seventy'
INSERT INTO @Table2 Select 'Eighty'
INSERT INTO @Table2 Select 'Ninety'
DECLARE @strWord VarChar(300)
Select @strWord = ''
DECLARE @iAmt1 BIGINT, @iAmt2 BIGINT
DECLARE @strWord1 VarChar(4000)
IF @Amt < 10000000000000 AND @Amt >= 100000000000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 100000000000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 100000000000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Kharab '
END
IF @Amt < 100000000000 AND @Amt >= 1000000000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 1000000000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 1000000000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Arab '
END
IF @Amt < 1000000000 AND @Amt >= 10000000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 10000000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 10000000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Crore '
END
IF @Amt < 10000000 AND @Amt >= 100000
BEGIN
SET @iAmt1 = @Amt
Select @Amt = ( @Amt % 100000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 100000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10)
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Lac '
END
IF @Amt < 100000 AND @Amt >= 1000
BEGIN
SET @iAmt1 = @Amt
SET @Amt = ( @Amt % 1000 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 1000
SET @strWord1 = ''
IF @iAmt1 < 100 AND @iAmt1 > 20
BEGIN
SET @iAmt2 = @iAmt1
SET @iAmt1 = ( @iAmt1 % 10 )
SET @iAmt2 = ( @iAmt2 - @iAmt1 ) / 10
SET @strWord1 = ( Select @strWord1 + AText + ' ' FROM @Table2 WHERE SlNo = @iAmt2 )
END
IF @iAmt1 <= 20 AND @iAmt1 <> 0
BEGIN
SET @strWord1 = ( Select @strWord1 + AText +' ' FROM @Table1 WHERE SlNo = @iAmt1 )
END
SET @strWord = @strWord + @strWord1 + ' Thousand '
END
IF @Amt < 1000 AND @Amt > = 100
BEGIN
SET @iAmt1 = @Amt
SET @Amt = ( @Amt % 100 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 100
SET @strWord = ( Select @strWord + ' ' +AText + ' Hundred ' FROM @Table1 WHERE SlNo = @iAmt1)
END
IF @Amt < 100 AND @Amt > 20
BEGIN
SET @iAmt1 = @Amt
SET @Amt = ( @Amt % 10 )
SET @iAmt1 = ( @iAmt1 - @Amt ) / 10
SET @strWord = ( Select @strWord + AText + ' ' FROM @Table2 WHERE SlNo = @iAmt1 )
END
IF @Amt <= 20 AND @Amt >= 1
BEGIN
SET @strWord = ( Select @strWord + AText +' ' FROM @Table1 WHERE SlNo = @Amt )
END
DECLARE @strWordP VarChar(300)
SET @strWordP = ''
IF @Paisa <> 0 BEGIN
IF @Paisa < 100 AND @Paisa > 20
BEGIN
DECLARE @Paisa_01 VarChar(300)
SET @Paisa_01 = @Paisa
SET @Paisa = ( @Paisa % 10 )
SET @Paisa_01 = ( @Paisa_01 - @Paisa ) / 10
SET @strWordP = ( Select @strWordP + AText FROM @Table2 WHERE SlNo = @Paisa_01 )
END
IF @Paisa <= 20 AND @Paisa >= 1
BEGIN
SET @strWordP = ( Select @strWordP + AText FROM @Table1 WHERE SlNo = @Paisa )
END
SET @strWord = @strWord + 'And ' + @strWordP + ' Paisa'
END
RETURN (REPLACE(@strWord, ' ', ' '))
END
GO
--Example
Select dbo.Amt_In_Words(6543211235412, 55)
No comments:
Post a Comment