Access query syntax for pulling 5 characters from the left of special charater in text field multiple times

Robert Cattanea 1 Reputation point
2021-11-24T15:43:19.557+00:00

Looking for access query syntax for selecting a 5 character string to the left of a ";" in data field but the field may have multiple cases. example of data field below;

52441;cysto with insertion 52442; cysto repair

Would like to append existing table with field showing cpt code and new line for each patient that has multiple cpt codes

Microsoft 365 and Office | Access | Development
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DBG 2,456 Reputation points Volunteer Moderator
    2021-11-24T18:56:00.953+00:00

    Hi. From the example data you posted, what part do you want to get back? 52441?

    Have you tried?

    Left([FieldName],5)

    0 comments No comments

  2. Gustav 717 Reputation points MVP
    2021-11-25T13:29:31.873+00:00

    You will need a small helper function:

    Public Function ExtractCode(ByVal Text As String, ByVal Item As Integer) As String
    
        Dim Values  As Variant
        Dim Value   As String
    
        Values = Split(Text, ";")
    
        If Item > LBound(Values) And Item <= UBound(Values) Then
            Value = Right(Trim(Values(Item - 1)), 5)
        End If
    
        ExtractCode = Value
    
    End Function
    

    Output:

    cptcodes = "52441;cysto with insertion 52442; cysto repair"
    
    ? ExtractCode(cptcodes, 1)
    52441
    
    ? ExtractCode(cptcodes, 2)
    52442
    

    In a query, you can pull the n'th value of the codes:

    Select *, ExtractCode([cptcodes], n) As cptcode From YourTable
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.