[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extract Characters From String Based On Format

Posted on 2011-03-08
13
Medium Priority
?
353 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:jonlake
  • 8
  • 3
  • 2
13 Comments
 
LVL 17

Expert Comment

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

Open in new window

0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35067128
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.
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35067133
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.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 17

Expert Comment

by:JezWalters
ID: 35067146
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

0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35067161
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?
0
 
LVL 77

Accepted Solution

by:
peter57r earned 2000 total points
ID: 35067183
Use this function in a query to extract the number.

It assumes the number is terminated with a "-".

Select *, getnos(Desc) from tablename



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 GetNos = res
End Function
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35067191
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

0
 
LVL 17

Expert Comment

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

Open in new window

0
 

Author Comment

by:jonlake
ID: 35067203
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.
0
 

Author Closing Comment

by:jonlake
ID: 35067218
Fantastic!

When I grow up I hope to be that good!

Have a great day.
0
 

Author Comment

by:jonlake
ID: 35067827
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?
0
 
LVL 77

Expert Comment

by:peter57r
ID: 35068851
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
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35069351
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

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question