Solved

Extract Characters From String Based On Format

Posted on 2011-03-08
13
320 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn off MS Access Default=0 for Numerics 6 27
trying to catch ODBC error when database opens 2 28
Problem copying record details to a new record 5 9
Filter a form 8 15
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

803 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