Technical Article

Converting money to the word value.

,

With this script you can supply an money value such as $12525.83 and it will return the word value "Twelve Thousand Five Hundred And Twenty-Five Dollars And Eighty-Three Cents".
It is comprised of a table for Number to Name and a procedure to parse into it's pieces
NOTE: This only goes to billions at this point, but easy enough to expand.
May work even better as a function in 2000.




Update Notice: If you tried this code before April 17, 2002 the following bug fixes have been made.

.00 throwing error has been fixed.

20.00 displaying "Twenty-Dollars" now corrrectly diplays "Twenty Dollars".

111111.25 Not showing values for Thousands, Millions, Billions with a 1 in the Tens place corrected.

Corrected single ones position for each place all doing single digits amount for place as double that same number, for example 2000 was outputting Twenty-Two Thousand.

Got rid of second "and" in output when there is cents so instead of 250.12 being "Two Hundred And Fifty Dollars And Twelve Cents" now outputs "Two Hundred Fifty Dollars And Twelve Cents" unless is 250 then is "Two Hundred And Fifty Dollars".

--You will need this table.--
CREATE TABLE NumNameTbl (
[val] [int] NOT NULL,
[numname] [varchar](20) NOT NULL,
CONSTRAINT [PK_NumNameTbl_val] PRIMARY KEY  CLUSTERED 
(
[val]
) WITH  FILLFACTOR = 100
)
GO 

/* Insert the value records, yes this is a heap table */INSERT INTO NumNameTbl (val,numname) VALUES (1,'One')
INSERT INTO NumNameTbl (val,numname) VALUES (2,'Two')
INSERT INTO NumNameTbl (val,numname) VALUES (3,'Three')
INSERT INTO NumNameTbl (val,numname) VALUES (4,'Four')
INSERT INTO NumNameTbl (val,numname) VALUES (5,'Five')
INSERT INTO NumNameTbl (val,numname) VALUES (6,'Six')
INSERT INTO NumNameTbl (val,numname) VALUES (7,'Seven')
INSERT INTO NumNameTbl (val,numname) VALUES (8,'Eight')
INSERT INTO NumNameTbl (val,numname) VALUES (9,'Nine')
INSERT INTO NumNameTbl (val,numname) VALUES (10,'Ten')
INSERT INTO NumNameTbl (val,numname) VALUES (11,'Eleven')
INSERT INTO NumNameTbl (val,numname) VALUES (12,'Twelve')
INSERT INTO NumNameTbl (val,numname) VALUES (13,'Thirteen')
INSERT INTO NumNameTbl (val,numname) VALUES (14,'Fourteen')
INSERT INTO NumNameTbl (val,numname) VALUES (15,'Fifteen')
INSERT INTO NumNameTbl (val,numname) VALUES (16,'Sixteen')
INSERT INTO NumNameTbl (val,numname) VALUES (17,'Seventeen')
INSERT INTO NumNameTbl (val,numname) VALUES (18,'Eightteen')
INSERT INTO NumNameTbl (val,numname) VALUES (19,'Nineteen')
INSERT INTO NumNameTbl (val,numname) VALUES (20,'Twenty')
INSERT INTO NumNameTbl (val,numname) VALUES (30,'Tirty')
INSERT INTO NumNameTbl (val,numname) VALUES (40,'Fourty')
INSERT INTO NumNameTbl (val,numname) VALUES (50,'Fifty')
INSERT INTO NumNameTbl (val,numname) VALUES (60,'Sixty')
INSERT INTO NumNameTbl (val,numname) VALUES (70,'Seventy')
INSERT INTO NumNameTbl (val,numname) VALUES (80,'Eighty')
INSERT INTO NumNameTbl (val,numname) VALUES (90,'Ninty')
GO

/* The procedure. */CREATE PROCEDURE ip_ReturnMoneyName
@Money Money
AS

SET NOCOUNT ON
DECLARE @MonName VARCHAR(8000)
DECLARE @MonStr VARCHAR(100)
DECLARE @TempMon VARCHAR(3)

SET @MonStr = CAST(@Money AS VARCHAR(100))

SET @MonName = ''

IF CAST(RIGHT(@MonStr,2) AS INT) > 0
BEGIN
SET @TempMon = RIGHT(@MonStr,2)

SET @MonName = 'Cents'

SELECT @MonName = numname + ' ' + @MonName FROM NumNameTbl WHERE val = (CASE LEFT(@TempMon,1) WHEN 1 THEN @TempMon ELSE RIGHT(@TempMon,1) END)

IF LEFT(@TempMon,1) NOT IN (0,1)
SELECT @MonName = numname + (CASE LTRIM(@MonName) WHEN 'CENTS' THEN ' ' ELSE '-' END) + @MonName FROM NumNameTbl WHERE val = CAST((LEFT(@TempMon,1) + '0') AS int)

IF LEN(@MonStr) = 4 AND LEFT(@MonStr,1) = 0
BEGIN
SET @MonName = @MonName
END
ELSE
SET @MonName = 'And ' + @MonName
END

SET @MonStr = LEFT(@MonStr,LEN(@MonStr) - 3)

/* Ones, Tens Hundreds */IF LEN(@MonStr) > 0
BEGIN
SET @TempMon = RIGHT(@MonStr,3)

SET @MonStr = LEFT(@MonStr, LEN(@MonStr) - LEN(@TempMon)) 

If CAST(@TempMon AS INT) != 0
BEGIN
SET @MonName = 'Dollars ' + @MonName

SELECT @MonName = numname + ' ' + @MonName FROM NumNameTbl WHERE val = (CASE LEFT(RIGHT(@TempMon,2),1) WHEN 1 THEN RIGHT(@TempMon,2) ELSE RIGHT(@TempMon,1) END)

IF LEN(@TempMon) > 1
BEGIN
IF LEFT(RIGHT(@TempMon,2),1) NOT IN (0,1)
SELECT @MonName = numname + (CASE RIGHT(@TempMon,1) WHEN 0 THEN ' ' ELSE '-' END) + @MonName FROM NumNameTbl WHERE val = CAST((LEFT(RIGHT(@TempMon,2),1) + '0') AS int)
END

IF CHARINDEX('Dollars',@MonName) > 1 AND (LEN(@TempMon) = 3 OR LEN(@MonStr) > 0)
IF CHARINDEX(' And ',@MonName) = 0
SET @MonName = 'And ' + @MonName

IF LEN(@TempMon) = 3 AND CAST(LEFT(@TempMon,1) AS INT) != 0
SELECT @MonName = numname + ' Hundred ' + @MonName FROM NumNameTbl WHERE val = CAST(LEFT(@TempMon,1) AS INT)
END

END

/* Thousands */IF LEN(@MonStr) > 0
BEGIN
SET @TempMon = RIGHT(@MonStr,3)

SET @MonStr = LEFT(@MonStr, LEN(@MonStr) - LEN(@TempMon)) 

If CAST(@TempMon AS INT) != 0
BEGIN
IF CHARINDEX('Dollars',@MonName) = 0
SET @MonName = 'Dollars ' + @MonName

SET @MonName = 'Thousand ' + @MonName

SELECT @MonName = numname + ' ' + @MonName FROM NumNameTbl WHERE val = (CASE LEFT(RIGHT(@TempMon,2),1) WHEN 1 THEN RIGHT(@TempMon,2) ELSE RIGHT(@TempMon,1) END)

IF LEN(@TempMon) > 1
BEGIN
IF LEFT(RIGHT(@TempMon,2),1) NOT IN (0,1)
SELECT @MonName = numname + (CASE RIGHT(@TempMon,1) WHEN 0 THEN ' ' ELSE '-' END) + @MonName FROM NumNameTbl WHERE val = CAST((LEFT(RIGHT(@TempMon,2),1) + '0') AS int)
END

IF LEN(@TempMon) = 3 AND CAST(LEFT(@TempMon,1) AS INT) != 0
SELECT @MonName = numname + ' Hundred ' + @MonName FROM NumNameTbl WHERE val = CAST(LEFT(@TempMon,1) AS INT)
END

END

/* Million */IF LEN(@MonStr) > 0
BEGIN
SET @TempMon = RIGHT(@MonStr,3)

SET @MonStr = LEFT(@MonStr, LEN(@MonStr) - LEN(@TempMon)) 

If CAST(@TempMon AS INT) != 0
BEGIN
IF CHARINDEX('Dollars',@MonName) = 0
SET @MonName = 'Dollars ' + @MonName

SET @MonName = 'Million ' + @MonName

SELECT @MonName = numname + ' ' + @MonName FROM NumNameTbl WHERE val = (CASE LEFT(RIGHT(@TempMon,2),1) WHEN 1 THEN RIGHT(@TempMon,2) ELSE RIGHT(@TempMon,1) END)

IF LEN(@TempMon) > 1
BEGIN
IF LEFT(RIGHT(@TempMon,2),1) NOT IN (0,1)
SELECT @MonName = numname + (CASE RIGHT(@TempMon,1) WHEN 0 THEN ' ' ELSE '-' END) + @MonName FROM NumNameTbl WHERE val = CAST((LEFT(RIGHT(@TempMon,2),1) + '0') AS int)
END

IF LEN(@TempMon) = 3 AND CAST(LEFT(@TempMon,1) AS INT) != 0
SELECT @MonName = numname + ' Hundred ' + @MonName FROM NumNameTbl WHERE val = CAST(LEFT(@TempMon,1) AS INT)
END

END

/* Billion */IF LEN(@MonStr) > 0
BEGIN
SET @TempMon = RIGHT(@MonStr,3)

SET @MonStr = LEFT(@MonStr, LEN(@MonStr) - LEN(@TempMon)) 

If CAST(@TempMon AS INT) != 0
BEGIN
IF CHARINDEX('Dollars',@MonName) = 0
SET @MonName = 'Dollars ' + @MonName

SET @MonName = 'Billion ' + @MonName

SELECT @MonName = numname + ' ' + @MonName FROM NumNameTbl WHERE val = (CASE LEFT(RIGHT(@TempMon,2),1) WHEN 1 THEN RIGHT(@TempMon,2) ELSE RIGHT(@TempMon,1) END)

IF LEN(@TempMon) > 1
BEGIN
IF LEFT(RIGHT(@TempMon,2),1) NOT IN (0,1)
SELECT @MonName = numname + (CASE RIGHT(@TempMon,1) WHEN 0 THEN ' ' ELSE '-' END) + @MonName FROM NumNameTbl WHERE val = CAST((LEFT(RIGHT(@TempMon,2),1) + '0') AS int)
END

IF LEN(@TempMon) = 3 AND CAST(LEFT(@TempMon,1) AS INT) != 0
SELECT @MonName = numname + ' Hundred ' + @MonName FROM NumNameTbl WHERE val = CAST(LEFT(@TempMon,1) AS INT)
END

END

IF CHARINDEX('One',@MonName) = 1 AND CHARINDEX('Dollars',@MonName) = 5
SET @MonName = REPLACE(@MonName,'Dollars','Dollar')

SELECT @MonName AS MoneyName
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating