| Author |
Topic |
|
V.V.
Starting Member
32 Posts |
Posted - 2011-10-28 : 04:44:56
|
| Guys,I need to remove from an address the postal code and I got stock on something.So, I have this code:SELECT...,replace(replace(replace(replace(client_address2,'Bloc','Bl.'),'Scara','Sc.'),'Etaj','Et.'),', Cod postal','') as address2from V1.dbo.AddressOk, everything works just fine until "Cod postal". In the address, "Cod postal" is like this "Cod postal xxxxxx" where x in (0-9). With replace function I can remove only "Cod postal". How can I remove the numeric part from the string also?I tried with "Cod postal%" but it doesn't work. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-28 : 04:57:30
|
| why are you using series of replaces? this code is not scalable as any change in format means you've to write a new set of code with replace functions. why not use patindex or charindex to shred string based on a delimiter if its consistent. if you can show some sample values, we will try to come up with a more generic and scalable solution.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-28 : 04:57:56
|
Have a look at STUFF() No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
V.V.
Starting Member
32 Posts |
Posted - 2011-10-28 : 05:20:01
|
| #£visakh16, I need to modify a procedure because it must export a .csv file. Ok, in this .csv file I have two columns:- column1 named Address1 contains the name of the street + the number of the street. (Ex: Str. Mainstreet, Nr. 1)- column2 named Address2 contains the block + the stair + the floor + the apartment + the postal code (Ex: Bl. A1, Sc. 1, Et. 1, Ap. 1, Cod postal 000001)The field separator between the components of the address (Address1 and Address2) is ",". I need to replace comma with space " " (because this is the original format of Address1 and Address2). The easiest way to do that was by replacing "," with " " and to short "Bloc" with "Bl", "Scara" with "Sc"... and so on.The issue is on "Cod postal" because I need to remove it from Address2 completely. That's why I got stock here.The original: Str. Mainstreet, Nr. 1, Bloc A1, Scara 1, Etaj 1, Ap. 1, Cod postal 000001 How I want to be: Str. Mainstreet Nr. 1 Bl. A1, Sc. 1 Et. 1 Ap. 1I hope I got myself clear now. I don't know how to explain other way. :) If you have a better solution, please let me know. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-28 : 05:31:34
|
ok if thats the case why not creating a mapping table (AddressMapping) and use it for the replace to make it generic likeAddressWord Code---------------- ----------------------Bloc BlScara ScEtaj Etthen use it likeUPDATE tSET t.Address1=REPLACE(REPLACE(t.Address1,',',' '),' ' + m.AddressPart + ' ',m.Code),t.Address2=REPLACE(REPLACE(t.Address2,',',' '),' ' + m.AddressPart + ' ',m.Code)FROM yourtable tINNER JOIN AddressMapping mON t.Address1 LIKE '% ' + m.AddressPart + ' %'OR t.Address2 LIKE '% ' + m.AddressPart + ' %'to make the changes and then use below to ignore numeric partUPDATE tSET t.Address2=LEFT(t.Address2,PATINDEX('%Codepostal%',t.Address2)-1)FROM yourtable tWHERE PATINDEX('%Codepostal%',t.Address2)>0I hope postal always comes last------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
V.V.
Starting Member
32 Posts |
Posted - 2011-10-28 : 05:53:55
|
| #£visakh16, wow! Thank you very much for this.The idea is that I need to modify today that procedure in the way I explain it. I don't have enough time to test you solution. :(I thought I could resolve it somehow with replace function. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-28 : 05:57:43
|
| ok. try the method. it will save lots of unnecessary replace statements and also make it generic so whenver you want make a word convertion just add it to table with corresponding code and no need to tweak your replace code to handle it------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
V.V.
Starting Member
32 Posts |
Posted - 2011-10-31 : 10:01:13
|
| #X002548, at that time I didn't have enough time to test #visakh16's solution. As you can see, I thank to him for it and I told him that I will test it after that.Have a nice day. :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-31 : 11:18:55
|
quote: Originally posted by V.V. #X002548, at that time I didn't have enough time to test #visakh16's solution. As you can see, I thank to him for it and I told him that I will test it after that.Have a nice day. :)
thats okhope it gave you what you were looking after ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|