Postgresql query to update fields using a regular expression

前端 未结 2 883
悲哀的现实
悲哀的现实 2021-02-19 17:30

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

123 Main Street

Using Postgresql, how would I write a query to update t

2条回答
  •  盖世英雄少女心
    2021-02-19 18:17

    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.

提交回复
热议问题