• Sql to extract numeric only (Access 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Sql to extract numeric only (Access 2003)

    Author
    Topic
    #452543

    Sample data:
    (Text Field)
    ABC123456
    123ABCDEF456
    0123ABCD456
    1234560ABCDE

    We are looking to extract the numeric portion of the above.
    We do not want leading zeros
    We DO WANT trailing zeros.

    The character count may be different for all data.

    Based on the above, we would want the following results:
    123456
    123456
    123456
    1234560

    Is there SQL code that can do this?

    I’ve tried using MID but am having a difficult time with it.

    Thank you for helping !

    Michael

    Viewing 1 reply thread
    Author
    Replies
    • #1117675

      I think you need a custom VBA function for this:

      Public Function ExtractNumber(strText As String) As Long
      Dim strRet As String
      Dim i As Integer
      Dim c As String
      For i = 1 To Len(strText)
      c = Mid(strText, i, 1)
      If c Like "[0-9]" Then
      strRet = strRet & c
      End If
      Next i
      ExtractNumber = CLng(strRet)
      End Function

      You can use this in a query as ExtractNumber([FieldName]), either as a calculated column in a select query or in the Update to line of an update query.

      • #1117682

        Hi Hans,

        I can’t wait to get back to work Monday to try it out !

        Thanks so much and have a nice weekend !!

        Michael

    • #1117982

      Hi Hans,
      I tried it using the data above and of course it works perfectly.

      There was one scenario that I did not plan on.

      If the field only has alpha characters, the update query gives a Run time 13 error Type mismatch.

      Sample data:
      (Text Field)
      ABC123456
      123ABCDEF456
      0123ABCD456
      1234560ABCDE
      ABCDEFG <———– bombs out here

      (Highlights this line of code: ExtractNumber = CLng(strRet)

      Is there a way around this?

      Thanks again for looking and assisting.

      Sincerely,
      Michael

      • #1117983

        What would you like the function to return in that situation – 0 (zero) or null (blank)?

        • #1117984

          Null would be perfect.

          • #1117987

            Try this version:

            Public Function ExtractNumber(strText As String) As Variant
            Dim strRet As String
            Dim i As Integer
            Dim c As String
            For i = 1 To Len(strText)
            c = Mid(strText, i, 1)
            If c Like "[0-9]" Then
            strRet = strRet & c
            End If
            Next i
            If strRet = "" Then
            ExtractNumber = Null
            Else
            ExtractNumber = CLng(strRet)
            End If
            End Function

            Note that the return type is now a Variant – this is necessary if you want to be able to return Null values.

            • #1117989

              Hans,

              Once again you did it. You are certainly a very nice man for sharing your vast knowledge with us.

              I thank you and appreciate your generosity.

              Michael

            • #1117991

              If there is a chance that the argument to the function will be null (blank), you can change the function to

              Public Function ExtractNumber(varText As Variant) As Variant
              Dim strRet As String
              Dim i As Integer
              Dim c As String
              If IsNull(varText) Then
              ExtractNumber = Null
              Exit Function
              End If
              For i = 1 To Len(varText)
              c = Mid(varText, i, 1)
              If c Like "[0-9]" Then
              strRet = strRet & c
              End If
              Next i
              If strRet = "" Then
              ExtractNumber = Null
              Else
              ExtractNumber = CLng(strRet)
              End If
              End Function

    Viewing 1 reply thread
    Reply To: Sql to extract numeric only (Access 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: