Results 1 to 4 of 4
  1. #1
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317

    Replacing a character depending on what surrounds it (SQL query question)

    Folks

    The following is a simplified version of a puzzle I'm faced with, but if I can solve the simplified version I'll be able to solve the real thing.

    Take the following column:

    MyField
    1-2A-B


    YZ-A6
    53-75-XC

    How within a query can I replace the hyphens with spaces but only where they appear between two numbers? So this is the result I'm after:

    MyField
    1 2A-B
    YZ-A6
    53 75-XC

    (Please note that the letters and numbers could be any values in any positions.)

    Thanks

    Remster

  2. #2
    qa12dx is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    india
    Posts
    112
    i think this will get you started on the path...

    SELECT Table2.MyField, InStr([MyField],"-") AS Expr1, Int(Mid([myfield],InStr([MyField],"-")-1,1)) AS Expr2, Int(Mid([myfield],InStr([MyField],"-")+1,1)) AS Expr3
    FROM Table2
    WHERE (((InStr([MyField],"-"))>0));

  3. #3
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Thanks, I'll give it a go.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I couldn't solve your problem strictly with a query, but maybe this will help.

    In a standard module, paste the following code:
    Code:
    Public Function ReplaceDash(pTheString) As String
        Dim sTemp As String
        Dim Pos As Integer
        Dim sChrBefore As String
        Dim sChrAfter As String
    
        sTemp = Trim(pTheString)
    
        Pos = InStr(1, [sTemp], "-")
        If Pos > 0 Then
            Do Until Pos = 0
                
                'get char before and after the dash
                sChrBefore = Mid([sTemp], Pos - 1, 1)
                sChrAfter = Mid([sTemp], Pos + 1, 1)
                
                'check to see if numbers
                If IsNumeric(sChrBefore) And IsNumeric(sChrAfter) Then
                'both numbers, replace dash with space
                    sTemp = Left(sTemp, Pos - 1) & " " & Mid(sTemp, Pos + 1)
                End If
                
                'do it again
                Pos = InStr(Pos + 1, [sTemp], "-")
            Loop
        End If
        
        ReplaceDash = sTemp
    
    End Function
    The query is
    Code:
    SELECT Table2.MyField, ReplaceDash([myfield]) AS [No Dash] FROM Table2;
    Change the blue text to your table/field names

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replacing numbers with text at the query level
    By lonesoac0 in forum Queries
    Replies: 4
    Last Post: 09-03-2014, 03:16 PM
  2. Replies: 3
    Last Post: 08-05-2014, 12:52 PM
  3. Replies: 15
    Last Post: 11-27-2013, 10:38 AM
  4. Replies: 6
    Last Post: 06-07-2013, 09:45 AM
  5. Replies: 6
    Last Post: 02-20-2013, 12:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums