Results 1 to 14 of 14
  1. #1
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9

    How do I extract only number from a field and store in a variable?


    I need to extract all the numbers from a field in ms access sql query and store them in a string for future use, is this possible?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    of course - look at using the val, mid, left and right functions or perhaps a combination of them

  3. #3
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9
    Here's my attempt at doing this:

    Code:
    SELECT Mid(Field1, 1, Len(Field1)) Like '%[0-9]%'  AS OnlyNumbers
    FROM Database;

  4. #4
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9
    Tried this and it only returns a single "0" :

    Code:
    select val ([field1]) Like '%[0-9]%' AS OnlyNumbers
    FROM Database;

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Can you give an example of what the data in [field1] looks like?

    If the numeric characters are in different places in the string with other characters between them, it might not be as easy as it looked.

  6. #6
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9
    Input:

    1234 Apple Hill Road.

    Output:

    1234

  7. #7
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9
    Some might be like this:

    Input:

    Po Box 1234

    Output:

    1234

  8. #8
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    See post #5 at http://www.utteraccess.com/forum/ind...wtopic=1952430.

    Though, I am curious to know the purpose of saving this number which in your example could be a street number or a po box or something else entirely.

  9. #9
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9
    @jwhite I need to do this in a MS Access SQL query, not vb. Eventually I will use the sql statment in vb as exactly that, a sql statement.

  10. #10
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    You put the code in a UDF (user defined function) and call the UDF from the sql statement...

  11. #11
    chrisetiquette is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    9
    @jwhite I'd rather not use a UDF if that's ok...

  12. #12
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Chris, since you're using the MS Access engine, you'll need to use a UDF. There are not built-in functions for stripping non-numeric characters from a variable/field. In other databases there are often regular expression functions that you could use (and you can actually implement regex functions using VBA UDFs), but in MS Access you can't get around using a UDF.



    Jeff

  13. #13
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    if the two examples are the only situation (i.e. a number at the beginning or end, but never in the middle) you could try something like

    iif(val(field1)<>0, val(field1),iif(val(strreverse(field1))<>0,strreve rse(val(strreverse(field1))) & ""),null))

  14. #14
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    Code:
    Public Function GetNumbersOnly(InputString As String) As String
      Dim i As Long
      For i = 1 To Len(InputString)
        If InStr("0123456789", Mid(InputString, i, 1)) > 0 Then
          GetNumbersOnly = GetNumbersOnly & Mid(InputString, i, 1)
        End If
      Next i
    End Function
    Then in your sql statement: GetNumbersOnly([YourFieldNameHere])

Please reply to this thread with any new information or opinions.

Similar Threads

  1. store table value in a variable
    By chromachem in forum Programming
    Replies: 3
    Last Post: 02-17-2016, 01:38 PM
  2. Store logged in username in a variable
    By Hammilton in forum Forms
    Replies: 11
    Last Post: 12-17-2015, 04:53 PM
  3. Date variable Store
    By drunkenneo in forum Programming
    Replies: 1
    Last Post: 07-01-2013, 05:57 AM
  4. Extract Value from Variable in String
    By nguyenak in forum Programming
    Replies: 3
    Last Post: 05-24-2012, 03:50 PM
  5. form with variable field number
    By FRSPA in forum Access
    Replies: 10
    Last Post: 05-04-2011, 05:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums