How to Replace Multiple Characters in Access SQL?

前端 未结 9 2196
北海茫月
北海茫月 2020-12-12 03:16

I\'m a novice at SQL, so hopefully someone can spell this out for me. I tried following the \"Replace Multiple Strings in SQL Query\" posting, but I got stuck.

I\'

相关标签:
9条回答
  • 2020-12-12 03:58

    You could use the built in Replace function within Access

    SELECT
        Replace(Replace(Replace(ShipToPlant, "#", ""), "-", ""), "/", "") AS ShipToPlant
    FROM
        BTST
    

    As others have said, within Access you can write your own functions in VBA and use them in your queries.

    EDIT:

    Here's a way to handle the nested Replace limit by wrappering the Replace function within our own function. It feels dirty but it works- put this in a module within Access

    Public Function SuperReplace(ByRef field As String, ByVal ReplaceString As String) As String
        ' Size this as big as you need... it is zero-based by default' 
        Dim ReplaceArray(3) As String
    
        'Fill each element with the character you need to replace'  
        ReplaceArray(0) = "#"
        ReplaceArray(1) = "-"
        ReplaceArray(2) = "/"
        ReplaceArray(3) = " "
    
        Dim i As Integer
        For i = LBound(ReplaceArray) To UBound(ReplaceArray)    
           field = Replace(field, ReplaceArray(i), ReplaceString)
        Next i
    
        SuperReplace = field    
    End Function
    

    Then test it with this query

    SELECT 
        SuperReplace(ShipToPlant,"") AS ShipToPlant
    FROM
        BTST
    

    You might want to take this an expand it so that you can pass in an array of strings instead of hard-coding them into the function.

    EDIT 2:

    In response to the additional information in the comments on the question, here's a suggestion for how you might want to handle the situation differently. The advantage to this apprach is that once you have mapped in a plant name permutation, you won't need to perform a string replace on future data in future years, only add new plant names and permutations to the map.

    Start with creating another table, let's call it plant_map

    CREATE TABLE plant_map (id AUTOINCREMENT PRIMARY KEY, name TEXT, master_id LONG)
    

    into plant_map, add all of the permutations for plant names and insert the id for the name you wish to use to refer to a particular plant name permutation group with, into the master_id field. From your comments, I'll use Signode Service

    INSERT INTO plant_map(name, master_id) VALUES ("Signode Service", 1);
    INSERT INTO plant_map(name, master_id) VALUES ("Signode Svc", 1);
    INSERT INTO plant_map(name, master_id) VALUES ("Signode - Service", 1);
    INSERT INTO plant_map(name, master_id) VALUES ("Signode svc", 1);
    INSERT INTO plant_map(name, master_id) VALUES ("SignodeService", 1);
    

    Now when you query BTST table, you can get data for Signode Service using

    SELECT
        field1,
        field2
    FROM
        BTST source
    INNER JOIN
        (
        plant_map map1      
        INNER JOIN
        plant_map map2
        ON map1.master_id = map2.id
        )
        ON source.ShipToPlant = map1.name
    WHERE
        map2.name = "Signode Service"
    

    Data within table BTST can remain unchanged.

    Essentially, this is joining on the plant name in BTST to the name in plant_map then, using master_id, self joining on id within plant_map so that you need only pass in one "common" name. I would advise putting an index on each of the columns name and master_id in plant_map as both fields will be used in joins.

    0 讨论(0)
  • 2020-12-12 04:04

    This query grabs the 3 first characters and replace them with Blanks

    Example: BO-1234
    Output: 1234

    BO: IIf(IsNumeric(Left([sMessageDetails],3)),[sMessageDetails],Replace([sMessageDetails],Left([sMessageDetails],3),""))
    
    0 讨论(0)
  • 2020-12-12 04:05

    Create a public function in a Code module.

    Public Function StripChars(ByVal pStringtoStrip As Variant, ByVal pCharsToKeep As String) As String
    
    Dim sChar As String
    Dim sTemp As String
    Dim iCtr As Integer
    
      sTemp = ""
    
      For iCtr = 1 To Len(pStringtoStrip)
        sChar = Mid(pStringtoStrip, iCtr, 1)
        If InStr(pCharsToKeep, sChar) > 0 Then
          sTemp = sTemp & sChar
        End If
      Next
    
      StripChars = sTemp
    
    End Function
    

    Then in your query

    SELECT
        StripChars(ShipToPlant, "abcdefghijklmnopqrstuvwxyz0123456789") AS ShipToPlantDisplay  
    FROM 
        BTST
    

    Notes - this will be slow for lots of records - if you what this to be permanent then create an update query using the same function.

    EDIT: to do an Update:

    UPDATE BTST
        SET ShipToPlant = StripChars(ShipToPlant, "abcdefghijklmnopqrstuvwxyz0123456789")
    
    0 讨论(0)
提交回复
热议问题