Results 1 to 7 of 7

Thread: How to change numbers to words in SQL?

  1. #1

    Thread Starter
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    How to change numbers to words in SQL?

    I want to display the numbers in Words in SQL

    e.g. 1204 should be displayed as One thousand and four..

    Any idea?

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    You mean you want the results of a query to do that? Are you using VB?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Not very easily.


    I wrote some code a while back for just this purpose, so you'll have to search the forums (button at top) and find it. Runs in VB/VBA.

    As to Sql doing it... not heard of that, but if you do manage to make it work, post up how you did it.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Try this

    I don't recall where this came from, so sorry I can't give credit where it's due.

    Edit:apparently from http://www.mentalis.org/tips/tip108.shtml

    Code:
    Public Function Number_To_Words(ByVal numstr As Long) As String
        ' The best data type to feed in is Decimal, but it is up to you
    
        Dim tempstr As String
        Dim newstr As String
        'numstr = CInt(numstr) 'CDec(numstr)
    
        If numstr = 0 Then
          Number_To_Words = "zero "
          Exit Function
        End If
    
        If numstr > 10 ^ 24 Then
          Number_To_Words = "Too big"
          Exit Function
        End If
    
        If numstr >= 10 ^ 12 Then
          newstr = Number_To_Words(Int(numstr / 10 ^ 12))
          numstr = ((numstr / 10 ^ 12) - _
          Int(numstr / 10 ^ 12)) * 10 ^ 12
          If numstr = 0 Then
            tempstr = tempstr & newstr & "billion "
          Else
            tempstr = tempstr & newstr & "billion, "
          End If
        End If
    
        If numstr >= 10 ^ 6 Then
          newstr = Number_To_Words(Int(numstr / 10 ^ 6))
          numstr = ((numstr / 10 ^ 6) - _
          Int(numstr / 10 ^ 6)) * 10 ^ 6
          If numstr = 0 Then
            tempstr = tempstr & newstr & "million "
          Else
            tempstr = tempstr & newstr & "million, "
          End If
        End If
    
        If numstr >= 10 ^ 3 Then
          newstr = Number_To_Words(Int(numstr / 10 ^ 3))
          numstr = ((numstr / 10 ^ 3) - _
          Int(numstr / 10 ^ 3)) * 10 ^ 3
          If numstr = 0 Then
            tempstr = tempstr & newstr & "thousand "
          Else
            tempstr = tempstr & newstr & "thousand, "
          End If
        End If
    
        If numstr >= 10 ^ 2 Then
          newstr = Number_To_Words(Int(numstr / 10 ^ 2))
          numstr = ((numstr / 10 ^ 2) - _
          Int(numstr / 10 ^ 2)) * 10 ^ 2
          If numstr = 0 Then
            tempstr = tempstr & newstr & "hundred "
          Else
            tempstr = tempstr & newstr & "hundred and "
          End If
        End If
    
        If numstr >= 20 Then
          Select Case Int(numstr / 10)
          Case 2
          tempstr = tempstr & "twenty "
          Case 3
          tempstr = tempstr & "thirty "
          Case 4
          tempstr = tempstr & "forty "
          Case 5
          tempstr = tempstr & "fifty "
          Case 6
          tempstr = tempstr & "sixty "
          Case 7
          tempstr = tempstr & "seventy "
          Case 8
          tempstr = tempstr & "eighty "
          Case 9
          tempstr = tempstr & "ninety "
          End Select
          numstr = ((numstr / 10) - _
          Int(numstr / 10)) * 10
        End If
    
        If numstr > 0 Then
          Select Case numstr
          Case 1
          tempstr = tempstr & "one "
          Case 2
          tempstr = tempstr & "two "
          Case 3
          tempstr = tempstr & "three "
          Case 4
          tempstr = tempstr & "four "
          Case 5
          tempstr = tempstr & "five "
          Case 6
          tempstr = tempstr & "six "
          Case 7
          tempstr = tempstr & "seven "
          Case 8
          tempstr = tempstr & "eight "
          Case 9
          tempstr = tempstr & "nine "
          Case 10
          tempstr = tempstr & "ten "
          Case 11
          tempstr = tempstr & "eleven "
          Case 12
          tempstr = tempstr & "twelve "
          Case 13
          tempstr = tempstr & "thirteen "
          Case 14
          tempstr = tempstr & "fourteen "
          Case 15
          tempstr = tempstr & "fifteen "
          Case 16
          tempstr = tempstr & "sixteen "
          Case 17
          tempstr = tempstr & "seventeen "
          Case 18
          tempstr = tempstr & "eighteen "
          Case 19
          tempstr = tempstr & "nineteen "
          End Select
          numstr = ((numstr / 10) - Int(numstr / 10)) * 10
        End If
    
        Number_To_Words = tempstr
    End Function    ' Number_To_Words
    '################################################################################
    Last edited by DaveBo; Jul 14th, 2004 at 09:40 AM.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    If I said One Thousand Two Hundred and Four, what would you think the number to be?
    1204 or 1200.4..... techically speaking the answer is 1200.4. The "and" signifies the decimal spot. So, to correctly do 1204 it is "One Thousand, Two Hundred Four." But that's not how most people speak.... But if you think about it, it makes sense.... 124 is one hundred twenty-four, not one hundred twenty and four.

    But eh....

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    VB Code:
    1. Public Function ConvertFromNumToString(ByVal dblAmount As Double) As String
    2. '---- Written by Vincent Buckner
    3. '----   10 May 2004
    4. '----
    5. '---- Requires:
    6. '----   A double number to convert
    7. '----
    8. '---- In Response to a forum post asking for this function
    9. '----   which I thought would be fun to make :)
    10.  
    11.     Dim aryBig() As String, aryNum() As String, aryTens() As String
    12.     Dim strAmount As String, strHolding As String, strLoop As String, strZeros As String
    13.     Dim dblInAmount As Double
    14.     Dim lngLoop As Long, lngMLoop As Long, lngAmount As Long
    15.     Dim blnAddAmType As Boolean, blnMinus As Boolean
    16.    
    17. '---- UK denomination - could pass as variables..?
    18.     Const strSep As String = "."
    19.     Const strMain As String = "pounds"
    20.     Const strLittle As String = "pence"
    21.    
    22.     On Error Resume Next
    23.  
    24.     ReDim aryBig(4)
    25.     aryBig(0) = ""
    26.     aryBig(1) = "Thousand"
    27.     aryBig(2) = "Million"
    28.     aryBig(3) = "Billion"
    29.     aryBig(4) = "Trillion"
    30.  
    31.     ReDim aryNum(19)
    32.     aryNum(0) = ""
    33.     aryNum(1) = "One"
    34.     aryNum(2) = "Two"
    35.     aryNum(3) = "Three"
    36.     aryNum(4) = "Four"
    37.     aryNum(5) = "Five"
    38.     aryNum(6) = "Six"
    39.     aryNum(7) = "Seven"
    40.     aryNum(8) = "Eight"
    41.     aryNum(9) = "Nine"
    42.     aryNum(10) = "Ten"
    43.     aryNum(11) = "Eleven"
    44.     aryNum(12) = "Twelve"
    45.     aryNum(13) = "Thirteen"
    46.     aryNum(14) = "Fourteen"
    47.     aryNum(15) = "Fifteen"
    48.     aryNum(16) = "Sixteen"
    49.     aryNum(17) = "Seventeen"
    50.     aryNum(18) = "Eighteen"
    51.     aryNum(19) = "Nineteen"
    52.    
    53.     ReDim aryTens(9)
    54.     aryTens(0) = ""
    55.     aryTens(1) = ""
    56.     aryTens(2) = "Twenty"
    57.     aryTens(3) = "Thirty"
    58.     aryTens(4) = "Fourty"
    59.     aryTens(5) = "Fifty"
    60.     aryTens(6) = "Sixty"
    61.     aryTens(7) = "Seventy"
    62.     aryTens(8) = "Eighty"
    63.     aryTens(9) = "Ninety"
    64.  
    65.  
    66.     ConvertFromNumToString = ""
    67.    
    68. '---- convert main section
    69.     blnAddAmType = False
    70.     blnMinus = False
    71.     If dblAmount < 0 Then
    72.         blnMinus = True
    73.         dblAmount = 0 - dblAmount
    74.     End If
    75.     strAmount = Format(dblAmount, "#0" & strSep & "00")
    76.     strAmount = Left$(strAmount, InStr(1, strAmount, strSep) - 1)
    77.     dblInAmount = CDbl(strAmount)
    78.    
    79. '    lngMLoop = Round(Len(CStr(dblInAmount)) / 3) + 1
    80.     lngMLoop = Int(Len(CStr(dblInAmount)) / 3) + IIf((Len(CStr(dblInAmount)) Mod 3) > 0, 1, 0)
    81.     strZeros = String(lngMLoop * 3, "0")
    82.     strAmount = Format(dblInAmount, strZeros)
    83.     For lngLoop = 1 To lngMLoop
    84.         strLoop = Format(CLng(Mid$(strAmount, ((lngLoop - 1) * 3) + 1, 3)), "000")
    85.         strHolding = ""
    86.        
    87. '---- deal with hundreds
    88.         If Left$(strLoop, 1) <> "0" Then
    89.             strHolding = strHolding & IIf(Len(strHolding) > 0, " and ", "") & aryNum(CLng(Left$(strLoop, 1))) & " Hundred"
    90.             blnAddAmType = True
    91.         End If
    92.        
    93. '------------------------------------------------
    94. '---- deal with the rest of the three digits ----
    95.         lngAmount = CLng(Right$(strLoop, 2))
    96.         If lngAmount > 0 Then
    97.             Select Case lngAmount
    98.                 Case Is < 20
    99.                     If Len(aryNum(lngAmount)) > 0 Then
    100.                         strHolding = strHolding & IIf(Len(strHolding) > 0, " and ", "") & aryNum(lngAmount)
    101.                     End If
    102.                 Case Else
    103.                     strHolding = strHolding & IIf(Len(strHolding) > 0, " and ", "") & aryTens(CLng(Left$(CStr(lngAmount), 1)))
    104.                     If CLng(Right$(CStr(lngAmount), 1)) > 0 Then
    105.                         strHolding = strHolding & IIf(Len(strHolding) > 0, "-", "") & aryNum(CLng(Right$(CStr(lngAmount), 1)))
    106.                     End If
    107.             End Select
    108.             If Len(strHolding) > 0 Then
    109.                 strHolding = strHolding & IIf(Len(aryBig(lngMLoop - lngLoop)) > 0, " ", "") & aryBig(lngMLoop - lngLoop)
    110.                 blnAddAmType = True
    111.             End If
    112.             ConvertFromNumToString = ConvertFromNumToString & IIf(Len(ConvertFromNumToString) > 0, ", ", "") & strHolding
    113.         End If
    114.     Next
    115.    
    116.     If blnAddAmType Then ConvertFromNumToString = ConvertFromNumToString & " " & strMain
    117.     strHolding = ""
    118.    
    119. '--------------------------------------
    120. '---- convert pence/cents whatever ----
    121.     strAmount = Format(dblAmount, "0" & strSep & "00")
    122.     dblInAmount = CLng(Right$(strAmount, 2))
    123.     If dblInAmount > 0 Then
    124.         Select Case dblInAmount
    125.             Case Is < 20
    126.                 strHolding = strHolding & IIf(Len(ConvertFromNumToString) > 0, " and ", "") & aryNum(dblInAmount)
    127.             Case Else
    128.                 strHolding = strHolding & IIf(Len(ConvertFromNumToString) > 0, " and ", "") & aryTens(CLng(Left$(CStr(dblInAmount), 1)))
    129.                 If Len(aryNum(CLng(Right$(CStr(dblInAmount), 1)))) > 0 Then
    130.                     strHolding = strHolding & IIf(Len(ConvertFromNumToString) > 0, "-", "") & aryNum(CLng(Right$(CStr(dblInAmount), 1)))
    131.                 End If
    132.         End Select
    133.         If Len(strHolding) > 0 Then strHolding = strHolding & " " & strLittle
    134.    
    135.         ConvertFromNumToString = ConvertFromNumToString & strHolding
    136.     End If
    137.    
    138.     If Len(ConvertFromNumToString) = 0 Then ConvertFromNumToString = "Nothing"
    139.     If blnMinus Then ConvertFromNumToString = "Minus " & ConvertFromNumToString
    140. End Function

    Prolly needs a little tweaking for american or other langs, but hopefully should be fairly easy to do.

    I was considering a pointless waste of time programming session to get this to run from text files, as in load in the lang you want and it fills the arrays as required.. but ATM ... its just that, a waste of time. I'll look into it next week

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7

    Thread Starter
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    Thanx all of ya.. for ur replies.....I got the sql solution for that..

    Select to_char(to_date(1234,'J'),'JSP') From Dual

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width