Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql query to update fields using a regular expression

I have the following data in my "Street_Address_1" column:

123 Main Street

Using Postgresql, how would I write a query to update the "Street_Name" column in my Address table? In other words, "Street_Name" is blank and I'd like to populate it with the street name value contained in the "Street_Address_1" column.

From what I can tell, I would want to use the "regexp_matches" string method. Unfortunately, I haven't had much luck.

NOTE: You can assume that all addresses are in a "StreetNumber StreetName StreetType" format.

like image 615
Huuuze Avatar asked Jun 01 '09 20:06

Huuuze


People also ask

Can you use regex in PostgreSQL?

The simplest use of regex in PostgreSQL is the ~ operator, and its cousin the ~* operator. value ~ regex tests the value on the left against the regex on the right and returns true if the regex can match within the value. Note that the regex does not have to fully match the whole value, it just has to match a part.

What does tilde mean in PostgreSQL?

PostgreSQL supports following four operators for POSIX regular expression matching (also known as the tilde operator). The tilde operator returns true or false depending on whether or not a regular expression can match a string or a part thereof.

How update works in Postgres?

PostgreSQL implements multiversioning by keeping the old version of the table row in the table – an UPDATE adds a new row version (“tuple”) of the row and marks the old version as invalid. In many respects, an UPDATE in PostgreSQL is not much different from a DELETE followed by an INSERT .


2 Answers

If you just want to take Street_Address_1 and strip out any leading numbers, you can do this:

UPDATE table
SET street_name = regexp_replace(street_address_1, '^[0-9]* ','','');

This takes the value in street_address_1 and replaces any leading string of numbers (plus a single space) with an empty string (the fourth parameter is for optional regex flags like "g" (global) and "i" (case-insensitive)).

This version allows things like "1212 15th Street" to work properly.

like image 44
Matthew Wood Avatar answered Sep 21 '22 17:09

Matthew Wood


Something like...:

UPDATE table
SET Street_Name = substring(Street_Address_1 FROM '^[0-9]+ ([a-zAZ]+) ')

See relevant section from PGSQL 8.3.7 docs, the substring form is detailed shortly after the start of the section.

like image 84
Alex Martelli Avatar answered Sep 21 '22 17:09

Alex Martelli