Link to home
Start Free TrialLog in
Avatar of jonlake
jonlakeFlag for Guernsey

asked on

Extract Characters From String Based On Format

Hi, using a query I need to be able to extract a code from within a character string. There can be several thousand lines, each with a different code but the format is always the same ####-. Here is an example:

Desc
10-403 Sal-RecruitInit 1485- Staff Recharge
10-413 Sales- Creative 1239- Specs UK advert
10-415 Marketing Man   1239- Specs UK advert
10-416 SaleProfPublica 1152- Professional Ma
10-416 SaleProfPublica 1153- Optom Career Pa
10-416 SaleProfPublica 1938- ODP Income
10-416 SaleProfPublica 1947- Nat Sem Inc
10-417 Sales - Recall  1239- Specs UK advert
10-421 Sales-Training  1048- Emplymnt Law Ma
10-421 Sales-Training  1049- Rectmnt Mat Inc
10-421 Sales-Training  1161- SmartBus RS In
10-421 Sales-Training  1215- Tra Fees - Gen
10-421 Sales-Training  1327- Conducting Staf

From line 1 I would be extracting 1485, line 2 1239 and so on.

Your help would be much appreciated.
Avatar of Jez Walters
Jez Walters
Flag of United Kingdom of Great Britain and Northern Ireland image

This should do what you're after:
SELECT YourField, Mid(YourField, InStrRev(YourField, "-") - 4, 4) AS Code
FROM YourTable

Open in new window

Mmmm, on second thoughts that won't work with this record

    10-421 Sales-Training  1215- Tra Fees - Gen

From the examples you've cited, you'll have to create a VBA function to parse the text, and invoke this from your query.
Can you confirm that the record values in your question are EXACTLY as you have listed them - including spaces and hyphens.

Once you've done this I can be confident that all cases are covered.
In particular, can you confirm if this record:

    10-413 Sales- Creative 1239- Specs UK advert

should actually be:

10-413 Sales-Creative 1239- Specs UK advert

Also, are there supposed to be two spaces before the code you're trying to extract in some records or is this just a typo?
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If your examples are correct, then you can extract the code by first creating the following function:
Option Explicit
Option Compare Database

Public Function ExtractCode(pstrText As String) As String

    Dim bytCharacter As Byte

    For bytCharacter = 7 To Len(pstrText)
        If IsNumeric(Mid(pstrText, bytCharacter, 1)) Then
            Exit For
        End If
    Next
    ExtractCode = Mid(pstrText, bytCharacter, 4)

End Function

Open in new window

You can then use the function in a query like this:
SELECT YourField, ExtractCode(YourField) AS Code
FROM YourTable

Open in new window

Avatar of jonlake

ASKER

Hi Jez, the leading spaces are contained in the csv file I import to begin the process. I can trim them so please consider the string minus the leading spaces.

Each line may vary as to the spaces contained in it, and the number of hyphens. The one consistent element is the ####- in the latter part of the string. The example you mentioned above does have an extra space behind Sales-. That will have been a user typo when the code string was created and isn't something I can alter.

So, I need to extract every string component formatted as ####-. I hope this helps.
Avatar of jonlake

ASKER

Fantastic!

When I grow up I hope to be that good!

Have a great day.
Avatar of jonlake

ASKER

Oops, just found a slight complication. Occasionally the code within the string is formatted as ####-####.

e.g. 2588-4876

In those cases I need the first four, and the second four.

Peter57r, can your code be modified/copied to split off the numbers on the right?
Use this to get either pattern:

Function GetNos(pstring As String) As String
Dim x As Integer, ln As Integer, ch As String
Dim res As String
GetNos = ""
ln = Len(pstring)
If ln < 7 Then Exit Function
For x = 6 To ln
    ch = Mid(pstring, x, 1)
    If ch >= "0" And ch <= "9" Then
        res = res & ch
    ElseIf Len(res) = 4 And ch = "-" Then
       
        Exit For
    Else
        res = ""
    End If
   
Next x
If Len(res) = 4 Then
    If Format(Val(Mid(pstring, x + 1, 4)), "0000") = Mid(pstring, x + 1, 4) Then res = res & "-" & Mid(pstring, x + 1, 4)
End If
 GetNos = res
End Function
Or like this:
Option Explicit
Option Compare Database

Public Function ExtractCode(pstrText As String) As String

    Dim bytCharacter As Byte
    Dim bytLength As Byte

    For bytCharacter = 7 To Len(pstrText)
        If IsNumeric(Mid(pstrText, bytCharacter, 1)) Then
            If IsNumeric(Mid(pstrText, bytCharacter + 5, 1)) Then
                bytLength = 9
            Else
                bytLength = 4
            End If
            Exit For
        End If
    Next
    ExtractCode = Mid(pstrText, bytCharacter, bytLength)

End Function

Open in new window