<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<br>
Tom Lane wrote:
<blockquote cite="mid:27615(dot)1239027099(at)sss(dot)pgh(dot)pa(dot)us" type="cite">
<pre wrap="">Tino Wildenhain <a class="moz-txt-link-rfc2396E" href="mailto:tino(at)living-examples(dot)com"><tino(at)living-examples(dot)com></a> writes:
</pre>
<blockquote type="cite">
<pre wrap="">I would not recommend to do this within the database. Thats typical
a job for your presentation layer.
</pre>
</blockquote>
<pre wrap=""><!---->
... but having said that, I think the "money" datatype has a function
for this. Whether that's of any use to you I dunno; money is pretty
restrictive about what it can handle.
regards, tom lane
</pre>
</blockquote>
<br>
I disagree the database is the wrong place, there are cases it makes
sense.<br>
<br>
I have looked for what Tom talks about for the money type i can't find
any thing?? so I wrote a function primary purpose is used with checks
but with a little modification will work for anyone one and has no
practical limitation . It will work to Quintillion. <br>
<br>
CREATE OR REPLACE FUNCTION spellNumericValue( pValue numeric)<br>
RETURNS text AS<br>
$BODY$<br>
DECLARE<br>
_dollar bigint = trunc(pValue)::text;<br>
_cents int = ((pValue - trunc(pValue))*100)::int;<br>
_spelledAmount text = '' ;<br>
_brokenOut int[] ;<br>
_pos integer = 0;<br>
_word text ;<br>
BEGIN<br>
<br>
--lets breakout the number into hundreds into a array<br>
WHILE _dollar > 0 loop<br>
_brokenOut = array_append(_brokenOut, (_dollar%1000)::int);<br>
_dollar = trunc(_dollar/1000);<br>
_pos = _pos + 1;<br>
End Loop;<br>
<br>
--this works on numbers between 1 to 999 transforming into english
words. then goes to the <br>
--next set of numbers in the array working backwards as the array
was loaded backwards<br>
--Meaning the highest value is in the last element of the array
_brokenOut<br>
--This also assumes words thousands millions, billions... occurs
every 10^3 .<br>
while _pos > 0 loop<br>
if _brokenOut[_pos] >99 then<br>
SELECT CASE <br>
WHEN _brokenOut[_pos] > 899 THEN 'Nine Hundred '<br>
WHEN _brokenOut[_pos] > 799 THEN 'Eight Hundred '<br>
WHEN _brokenOut[_pos] > 699 THEN 'Seven Hundred '<br>
WHEN _brokenOut[_pos] > 599 THEN 'Six Hundred ' <br>
WHEN _brokenOut[_pos] > 499 THEN 'Five Hundred '<br>
WHEN _brokenOut[_pos] > 399 THEN 'Four Hundred '<br>
WHEN _brokenOut[_pos] > 299 THEN 'Three Hundred '<br>
WHEN _brokenOut[_pos] > 199 THEN 'Two Hundred '<br>
WHEN _brokenOut[_pos] > 99 THEN 'One Hundred '<br>
else ''<br>
end<br>
into _word;<br>
_spelledAmount = _spelledAmount || _word ;<br>
end if;<br>
<br>
Select Case<br>
WHEN _brokenOut[_pos]%100 = 10 THEN 'Ten '<br>
WHEN _brokenOut[_pos]%100 = 11 THEN 'Eleve '<br>
WHEN _brokenOut[_pos]%100 = 12 THEN 'Twelve '<br>
WHEN _brokenOut[_pos]%100 = 13 THEN 'Thirteen '<br>
WHEN _brokenOut[_pos]%100 = 14 THEN 'Fourteen '<br>
WHEN _brokenOut[_pos]%100 = 15 THEN 'Fifteen '<br>
WHEN _brokenOut[_pos]%100 = 16 THEN 'Sixteen '<br>
WHEN _brokenOut[_pos]%100 = 17 THEN 'Seventeen '<br>
WHEN _brokenOut[_pos]%100 = 18 THEN 'Eighteen'<br>
WHEN _brokenOut[_pos]%100 = 19 THEN 'Nineteen '<br>
WHEN _brokenOut[_pos]/10%10=2 THEN 'Twenty ' <br>
WHEN _brokenOut[_pos]/10%10=3 THEN 'Thirty ' <br>
WHEN _brokenOut[_pos]/10%10=4 THEN 'Fourty ' <br>
WHEN _brokenOut[_pos]/10%10=5 THEN 'Fifty ' <br>
WHEN _brokenOut[_pos]/10%10=6 THEN 'Sixty ' <br>
WHEN _brokenOut[_pos]/10%10=7 THEN 'Seventy ' <br>
WHEN _brokenOut[_pos]/10%10=8 THEN 'Eighty ' <br>
WHEN _brokenOut[_pos]/10%10=9 THEN 'Ninety ' <br>
ELSE ''<br>
End<br>
into _word;<br>
_spelledAmount = _spelledAmount || _word;<br>
<br>
if _brokenOut[_pos]%100 < 10 or _brokenOut[_pos]%100 > 20
then<br>
SELECT CASE <br>
WHEN _brokenOut[_pos]%10 = 1 THEN 'One '<br>
WHEN _brokenOut[_pos]%10 = 2 THEN 'Two'<br>
WHEN _brokenOut[_pos]%10 = 3 THEN 'Three '<br>
WHEN _brokenOut[_pos]%10 = 4 THEN 'Four '<br>
WHEN _brokenOut[_pos]%10 = 5 THEN 'Five '<br>
WHEN _brokenOut[_pos]%10 = 6 THEN 'Six '<br>
WHEN _brokenOut[_pos]%10 = 7 THEN 'Seven '<br>
WHEN _brokenOut[_pos]%10 = 8 THEN 'Eight '<br>
WHEN _brokenOut[_pos]%10 = 9 THEN 'Nine '<br>
ELSE ''<br>
end<br>
into _word;<br>
<br>
_spelledAmount = _spelledAmount || _word;<br>
end if ;<br>
<br>
If _pos = 2 then<br>
_spelledAmount = _spelledAmount || 'Thousand ';<br>
elsif _pos = 3 then<br>
_spelledAmount = _spelledAmount || 'Million';<br>
elsif _pos = 4 then<br>
_spelledAmount = _spelledAmount || 'Billion ';<br>
elsif _pos = 5 then<br>
_spelledAmount = _spelledAmount || 'Trillion ';<br>
elsif _pos = 6 then<br>
_spelledAmount = _spelledAmount || 'Quadrillion ';<br>
elsif _pos = 7 then<br>
_spelledAmount = _spelledAmount || 'Quintillion ';<br>
else <br>
_spelledAmount = _spelledAmount || '';<br>
end if;<br>
<br>
_pos = _pos-1;<br>
end loop;<br>
<br>
if _cents = 0 then<br>
_spelledAmount = _spelledAmount || ' and Zero cents';<br>
else<br>
_spelledAmount = _spelledAmount || 'and ' || _cents::text ||
'/100 cents';<br>
end if ;<br>
return _SpelledAmount;<br>
<br>
END;<br>
$BODY$<br>
LANGUAGE 'plpgsql' VOLATILE<br>
<br>
<br>
</body>
</html>