Frage

I am having a Dirty database and i have to clean it, by first extracting city name from the address (which is written with several variation) and then standardize spelling the city name and replace all the data with standardize city name

I have made a Cities_look up table. in which all possible city name variations are written this table is having 2 columns for example

Standard_City_Name  Dirty_City Name
NEW YORK             NEW
NEW YORK             NY
NEW YORK             newyork
NEW YORK             New york
NEW YORK             ny
NEW YORK             NWYK

now i have to Trim city name from the address by looking up in this table comparing all the values in Dirty_City_Name column then triming it out. and then make new column of city and write there the Standard_City_Name

i am ding this

SELECT TRIM(TRIM(TRAILING Cities_lookup.Dirty_City_Name FROM Address) ), Cities_lookup.Dirty_City_Name
from Student left join Cities_lookup 
 on Student.Address like CONCAT('% ',Cities_lookup.Dirty_City_Name)

Can anyone help?

War es hilfreich?

Lösung

Because you know the address is at the end of the string, you can use a trick to replace it via stuff(). That is, just replace the characters in those positions with the empty string.

I find the easiest way to do this is by reversing the string, replacing the first +1 characters, and reversing again. In this case, is the length of the dirty city name and the "+ 1" is for the space:

select reverse(stuff(reverse(Address), 1, len(cl.Dirty_City_Name) + 1, '')), cl.Standard_City_Name
from Student s left join
     Cities_lookup cl
     on s.Address like '% '+cl.Dirty_City_Name
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top