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\'
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.
SELECT
IIF
(
Instr(1,ShipToPlant , "#") > 0
OR Instr(1,ShipToPlant , "/") > 0
OR Instr(1,ShipToPlant , "-") > 0, ""
, ShipToPlant
)
FROM BTST
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!
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
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
....
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:
char
for this example 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.:
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