Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Extract Characters From String Based On Format

Posted on 2011-03-08
13
Medium Priority
?
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

636 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