0

I have a lot of rows with city and direction fields. But from an old import, city and direction are mixed in direction field. Like:

dir number,  extra data, CITY,  AL 111111
dir number, CITY,  AL 111111
number, dir, number, CITY, dir number, CITY,  AL 111111

The important thing is that 'CITY' always comes before a US postal code and I want to extract it and save it in city field with an UPDATE (using regex?). Is it possible?

Something like:

update TABLE set city = SOME_REGEX_MAGIC_FROM_DIRECTION_FIELD
where direccion ~ 'REGEX_MAGIC'

The working SQL statement:

update TABLE
set city = substring(direction FROM '([^,]+),[^,]+$')
where direction like '%,  __ _____';
0

3 Answers 3

3

If you want the part before the last comma, one way (of many) is with a plain substring() call (regexp variant):

substring(direction FROM ',([^,]+),[^,]+$') AS city

db<>fiddle here

Your UPDATE statement might look like this:

UPDATE tbl
SET    city = substring(direction FROM ',([^,]+),[^,]+$')
WHERE  direction ~ ', *\D\D \d{5}$'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Excuse my english if I didn't explained well. Your regex work great. I always forget about negation in regex and it solved a lot of problems :) I also saw there is another records like "city, XX 99999", with a small change to your regex it work like a charm. The working SQL statement: update TABLE set city = substring(direction FROM '([^,]+),[^,]+$') where direction like '%, __ _____' 102051 registers changed :)
0

From your data I would gather that you'll need something like this:

SELECT regexp_matches('direction_field', '([^,]+) \d{5}');

1 Comment

Wolph, thanks, but it didn't work, direction field is more complex and there is postal code repeated and it extract the region code not the city
-1

Get a substring from a regex in Redshift:

SELECT REGEXP_SUBSTR(
   'hello_uuid_092bab12-8d8b-40ad-b8b7-bc9f05e52c9c_something_else',
   '([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})'
)

Result: 092bab12-8d8b-40ad-b8b7-bc9f05e52c9c

Comments

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.