1

I have a address column has values + in postal code. But + could be anywhere in the column (Street name, city) but I need to identity only postal code in the address with another column called Postal_Code and remove +.

It is going to be hard sql query to identify + in postal code and remove but don't know how to do?

I already remove + from Postal_Code column.

Table Example

POSTAL_CODE             FORMATTED_ADDRESS
T2E 8N6                 #6, 1435-40th Ave NE #6 Calgary AB  T2E+8N6 Canada
23401                   9 Abolaji Street Mafoluku +23401 Oshodi Lagos State Nigeria
234-01                  6 Force Road, Square Race Course +234-01 Lagos, Nigeria Lagos State
86 163453               Daqing Oilfield Co.Ltd Ranghulu 86+163453 Daqing CHN
3
  • Are you looking update rows that are selected with something along the lines of: select * from my_table where FORMATTED_ADDRESS like '%+' + POSTAL_CODE + '%? Commented Jul 5, 2018 at 16:07
  • Do you want to UPDATE the FORMATTED_ADDRESS and remove those + if it's in front of the matching POSTAL_CODE? Or just SELECT them without the + ? Commented Jul 5, 2018 at 16:08
  • @LukStorms update formatted_address Commented Jul 5, 2018 at 16:09

2 Answers 2

2

This seems hard in MS SQL Server.
(this would be so much easier with a regexp_replace function...)

Anyway....

Example snippet:

declare @YourTable table (POSTAL_CODE varchar(30), FORMATTED_ADDRESS varchar(1000));

insert into @YourTable (POSTAL_CODE, FORMATTED_ADDRESS) values
('T2E 8N6','#6, 1435-40th Ave NE #6 Calgary AB  T2E+8N6 Canada'),
('23401', '9 Abolaji Street Mafoluku +23401 Oshodi Lagos State Nigeria'),
('234-01', '6 Force Road, Square Race Course +234-01 Lagos, Nigeria Lagos State'),
('86 163453', 'Daqing Oilfield Co.Ltd Ranghulu 86+163453 Daqing CHN'),
(NULL, 'foo +1234 bar'),
('12345', 'bar 12345+ baz');

UPDATE @YourTable
SET FORMATTED_ADDRESS = 
    REPLACE(
       REPLACE(
          REPLACE(FORMATTED_ADDRESS, 
             '+'+POSTAL_CODE, POSTAL_CODE),
             POSTAL_CODE+'+', POSTAL_CODE),
             REPLACE(POSTAL_CODE,' ','+'), POSTAL_CODE)
WHERE POSTAL_CODE IS NOT NULL
AND (
     FORMATTED_ADDRESS LIKE CONCAT('%+', POSTAL_CODE, '%') OR
     FORMATTED_ADDRESS LIKE CONCAT('%', POSTAL_CODE, '+%') OR
     (POSTAL_CODE LIKE '%[ ]%' AND FORMATTED_ADDRESS LIKE CONCAT('%', REPLACE(POSTAL_CODE,' ','+'), '%'))
);


SELECT * FROM @YourTable;
Sign up to request clarification or add additional context in comments.

4 Comments

your code working perfectly good. only one condition it is not working when Mafoluku 23401+ Oshodi . Front + working but not rear + not
And also if your update state setting NULL if POSTAL_CODE is null
@James123 No longer selecting the empty postal_codes. And now also with + after it.
@James123 it's been simplified.
0

This can be done with some REPLACE statements:

UPDATE YourTable
SET FORMATTED_ADDRESS = REPLACE(
        REPLACE(FORMATTED_ADDRESS, REPLACE(POSTAL_CODE, ' ', '+'), POSTAL_CODE), 
        '+'+POSTAL_CODE, 
        POSTAL_CODE)
FROM YourTable

You can test the results of this query by SELECTing first:

SELECT REPLACE(
        REPLACE(FORMATTED_ADDRESS, REPLACE(POSTAL_CODE, ' ', '+'), POSTAL_CODE), 
        '+'+POSTAL_CODE, 
        POSTAL_CODE) AS NEW_FORMATTED_ADDRESS,
        FORMATTED_ADDRESS
FROM YourTable

2 Comments

How to check this query works to see not breaking anything.??
Check the edit, you can see what the UPDATE will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.