Solved

Convert Dollars Amount Into Words

Posted on 2004-09-22
7
1,052 Views
Last Modified: 2012-06-27
Hello EE,

I am being presenting with a simple, yet challenging issue and would like to ask all EE to see if anyone know of a SQL function or if anyone know of a good way handle this.

myTable

Rec      Col1           Col2
1     10911.95        
2       3181.00
3         506.09
4           40.14
5            0.85

Col1 is money field,  run an Update statement for Col2 in myTable

Results after update

Rec     Col1           Col2
1     10911.95       Ten Thousand Nine Hundred Eleven and 95/100 Dollars
2       3181.00       Three Thousand One Hundred One and 00/100 Dollars
3         506.09       Five Hundred Six and 09/100 Dollars
4           40.14       Forty and 14/100 Dollars
5             0.85       Zero and 85/100 Dollars

As you can tell, it is very much like the way we all are writing our personal check.

Thank you!



0
Comment
Question by:MartinCMS
  • 3
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
ChrisFretwell earned 500 total points
ID: 12124386
This must have been a common question. You can find code (posted today) for a function to do exactly this at

http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=5591

I didnt write this, so no points please if you like the function.
0
 
LVL 8

Author Comment

by:MartinCMS
ID: 12126524
@ChrisFretwell  - The function work great up to Hundred Thousand.  I have to make some minor modification to get it to works up to Hundred Billion the way I envision it.

I know you said no point, but you're the one leading me to get to the answer is as good as helping me accomplished the solution.  So the points are all your, thank you very much @ChrisFretwell.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 12126773
Darn it, I'm too slow!  

I just finished a version using two functions (a subfunc handled 1-999 since that is common to all amount levels: billions, millions, thousands, etc.).
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 8

Author Comment

by:MartinCMS
ID: 12126824
@ScottPletcher - I wouldn't mind to learn more than one way to accomplish this task at all.  How about 250 points to see your logic?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 12126931
Works for me.

I, too, have seen this Q posted before and finally had time to work on it :-).  As a side benefit, function is customized to what you indicated as your desired output.

Sample invocation:

SELECT dbo.numberToWords(123456789123.45)
SELECT dbo.numberToWords(0.00)



-- sub-function (/ "worker" function)

CREATE FUNCTION dbo.Number999ToWords (
      @amount INT
)
RETURNS VARCHAR(36)
AS
BEGIN --FUNCTION

IF @amount = 0
      RETURN ''

DECLARE @result VARCHAR(36)
SET @result = ''

IF @amount > 99
BEGIN
      SET @result = @result + RTRIM(SUBSTRING('One  Two  ThreeFour Five Six  SevenEightNine',
                  ((@amount / 100) - 1) * 5 + 1, 5)) + ' Hundred '
      SET @amount = @amount % 100
END --IF

IF @amount BETWEEN 11 AND 19
BEGIN
      SET @result = @result + RTRIM(SUBSTRING(
            'Eleven   Twelve   Thirteen Fourteen Fifteen  Sixteen  SeventeenEighteen Nineteen',
            ((@amount - 10) - 1) * 9 + 1, 9))
END --IF
ELSE
BEGIN
      IF @amount > 9
            SET @result = @result + RTRIM(SUBSTRING(
                  'Ten    Twenty Thirty Forty  Fifty  Sixty  SeventyEighty Ninety',
                  ((@amount / 10) - 1) * 7 + 1, 7))
      IF @amount % 10 <> 0
      BEGIN
            IF @amount > 9
                  SET @result = @result + '-'
            SET @result = @result + RTRIM(SUBSTRING('One  Two  ThreeFour Five Six  SevenEightNine',
                  ((@amount % 10) - 1) * 5 + 1, 5))
      END --IF
END --ELSE
RETURN RTRIM(@result)
END --FUNCTION



-- main function

CREATE FUNCTION dbo.NumberToWords (
      @amount DECIMAL(14, 2)
)
RETURNS VARCHAR(150)
AS
BEGIN --FUNCTION

DECLARE @result VARCHAR(150)
SET @result = ''

IF @amount < 1.00
      SET @result = 'Zero'

IF @amount > 999999999
BEGIN
      SET @result = @result + dbo.Number999ToWords(@amount / 1000000000) + ' Billion '
      SET @amount = @amount - (FLOOR(@amount / 1000000000) * 1000000000)
END --IF

IF @amount > 999999
BEGIN
      SET @result = @result + dbo.Number999ToWords(@amount / 1000000) + ' Million '
      SET @amount = @amount - (FLOOR(@amount / 1000000) * 1000000)
END --IF

IF @amount > 999
BEGIN
      SET @result = @result + dbo.Number999ToWords(@amount / 1000) + ' Thousand '
      SET @amount = @amount - (FLOOR(@amount / 1000) * 1000)
END --IF

SET @result = @result + dbo.Number999ToWords(@amount) + ' '
SET @amount = @amount - FLOOR(@amount)

SET @result = @result + 'and ' + RIGHT('0' + CAST(CAST(@amount * 100 AS SMALLINT) AS VARCHAR(2)), 2) + '/100 Dollars '

RETURN @result
END --FUNCTION
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 12127017
I have to admit I couldn't tell if I got errors from the other function (the linked-to code).  Did I feed the value in in the wrong format?

SELECT dbo.no2word('123456789')

Result:
Twelve Crore Thirty Four Lack Fifty Six Thousand Seven Hundred Eighty Nine

??
0
 
LVL 8

Author Comment

by:MartinCMS
ID: 12127166
@ScottPletcher - you're absolutely right!  

SELECT dbo.no2word('123456789') function actually returned that result which was why I have to make modification to get it to work.  Anyway, your function works wonder!  I can feed decimal and get the result as expected.  

I'll post points for you shortly. Nice code @ScottPletcher!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now