简体   繁体   中英

Deleting values and splitting values from single column to multiple ones in SQL

I have a column named STREETNAME where I have data based on this model: townName , streetName , 1 where 1 is the number of a house of a certain streetName . How do I delete townName from the whole column and also put numbers of houses into a separate column named STREETNO ?

I would suggest regexp_substr() . This seems like the simplest method in Oracle to split a string with components separated as commas:

select regexp_substr(streetname, '[^,]+', 1, 1) as town,
       regexp_substr(streetname, '[^,]+', 1, 2) as street,
       regexp_substr(streetname, '[^,]+', 1, 3) as num
from (select 'town, street, 123' as streetname from dual) x

You could try using REGEXP_REPLACE here:

SELECT
    REGEXP_REPLACE(STREETNAME, '^[^,]+, (.*)$', '\1') AS STREETNAME_NO_TOWN,
    REGEXP_REPLACE(STREETNAME, '^.* (.*)$', '\1') AS STREETNO
FROM yourTable;

The above alias STREETNAME_NO_TOWN would only remove the town name (which is what your question asked for), thereby leaving the street and number. If instead you want to isolate only the street then use this:

REGEXP_REPLACE(STREETNAME, '^[^,]+, ([^,]+),.*$', '\1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM