How to Replace Multiple Characters in Access SQL?

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

提交回复
热议问题