How to Replace Multiple Characters in Access SQL?

前端 未结 9 2195
北海茫月
北海茫月 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:41

    I know this is a really old question, but I stumbled over it whilst looking for a solution to this problem, but ended up using a different approach.

    The field that I wish to update is called 'Customers'. There are 20-odd accented characters in the 'CustName' field for which I wish to remove the diacritics - so (for example) ã > a.

    For each of these characters I created a new table 'recodes' with 2 fields 'char' and 'recode'. 'char' contains the character I wish to remove, and 'recode' houses the replacement.

    Then for the replace I did a full outer join inside the update statement

    UPDATE Customers, Recodes SET Customers.CustName = Replace([CustName],[char],[recode]);
    

    This has the same effect as nesting all of the replace statements, and is a lot easier to manage.

    0 讨论(0)
  • 2020-12-12 03:44
    
    SELECT 
    IIF
    (
        Instr(1,ShipToPlant , "#") > 0 
        OR Instr(1,ShipToPlant , "/") > 0 
        OR Instr(1,ShipToPlant , "-") > 0, ""
        , ShipToPlant 
    )
    FROM BTST
    
    
    0 讨论(0)
  • 2020-12-12 03:44

    All - I wound up nesting the REPLACE() function in two separate queries. Since there's upwards of 35 non-alphanumeric characters that I needed to replace and Access limits the complexity of the query to somewhere around 20 nested functions, I merely split it into two processes. Somewhat clunky, but it worked. Should have followed the KISS principle in this case. Thanks for your help!

    0 讨论(0)
  • 2020-12-12 03:46

    You may wish to consider a User Defined Function (UDF)

    SELECT ShiptoPlant, CleanString([ShiptoPlant]) AS Clean
    FROM Table
    
    
    Function CleanString(strText)
    Dim objRegEx As Object
    
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.IgnoreCase = True
    objRegEx.Global = True
    
    objRegEx.Pattern = "[^a-z0-9]"
    CleanString = objRegEx.Replace(strText, "")
    
    End Function
    
    0 讨论(0)
  • 2020-12-12 03:51

    Don't think Access supports the CASE statement. Consider using iif:

    iif ( condition, value_if_true, value_if_false )
    

    For this case you can use the REPLACE function:

    SELECT 
        REPLACE(REPLACE(REPLACE(yourfield, '#', ''), '-', ''), '/', '') 
        as FieldName
    FROM
        ....
    
    0 讨论(0)
  • 2020-12-12 03:51

    OK, your question has changed, so the solution will too. Here are two ways to do it. The quick and dirty way will only partially solve your issue because it won't be able to account for the more odd permutations like missing spaces or misspelled words. The quick and dirty way:

    1. Create a new table - let's call it tChar.
    2. Put a text field in it - the char(s) you want to replace - we'll call it char for this example
    3. Put all the char or char combinatios that you want removed in this table.
    4. Create and run the query below. Note that it will only remove one item at a time, but you can also put different versions of the same replacement in it too like ' -' or '-' For this example I created a table called tPlant with a field called ShipToPlant.

      SELECT tPlant.ShipToPlant, Replace([ShipToPlant], (SELECT top 1 char FROM tChar WHERE instr(ShipToPlant,char)<>0 ORDER BY len(char) Desc),"" ) AS New FROM tPlant;

    The better (but much more complex) way. This explanation is going to be general because it would be next to impossible to put the whole thing in here. If you want to contact me directly use my user name at gmail.:

    1. Create a table of Qualifiers - mistakes that people enter like svc instead of service. Here you would enter every wierd permutation you get.
    2. Create a table with QualifierID and Plant ID. Here you would say which qualifier goes to which plant.
    3. Create a query that joins the two and your table with mistaken plant names in it. Use instr so say what is in the fields.
    4. Create a second query that aggragates the first. Count the instr field and use it as a score. The entry with the highest score is the plant.
    5. You will have to hand enter the ones it can't find, but pretty soon that will be next to none as you have more and more entries in the table.

    ughh


    You have a couple different choices. In Access there is no CASE in sql, you need to use IIF. It's not quite as elegant as the solutions in the more robust db engines and needs to be nested for this instance, but it will get the job done for you.

    SELECT
        iif(instr(ShipToPlant,"#")<>0,"",
        iif(instr(ShipToPlant,"-")<>0,"",
        iif(instr(ShipToPlant,"/")<>0,"",ShipToPlant ))) AS FieldName
    FROM BTST;
    

    You could also do it using the sql to limit your data.

    SELECT YourID, nz(aBTST.ShipToPlant,"") AS ShipToPlant  
    FROM BTST LEFT JOIN (
        SELECT YourID, ShipToPlant 
        FROM BTST 
        WHERE ShipToPlant NOT IN("#", "-", "/")
        ) as aBTST ON BTST.YourID=aBTST.YourID
    

    If you know VB you can also create your own functions and put them in the queries...but that is another post. :) HTH

    0 讨论(0)
提交回复
热议问题