简体   繁体   中英

How do I Select a substring in Oracle SQL after a specific word in the string?

I am trying to extract everything in the following string after the word 'Hændelse'.

string:

Recordtype001[ efternavn = Nymark.] Adr2: 7020 Trondheim Hændelse A06: Udrejse er modtaget fra CPR-registeret.

I have tried to use REGEXP_SUBSTR - but it just returns the end of the string 't."'

I would like it to return

A06: Udrejse er modtaget fra CPR-registeret.

This is what I have tried:

WITH data AS
 (
    SELECT '"Recordtype001[ efternavn = Nymark.] Adr2: 7020 Trondheim Hændelse A06: Udrejse er modtaget fra CPR-registeret."' string 
    FROM dual
 )
 SELECT string,
        REGEXP_SUBSTR(string, '[^Hændelse]+$') new_string
 FROM data;

Your regex does not do what you want. You are defining a character class that must not contain characters from string ' Hændelse' . Basically it is looking for a series of characters other than 'H' , 'æ' , 'n' , 'd' , 'e' , 'l' , 's' , 'e' at the end of the string.

One option is regexp_replace() :

regexp_replace(string, '^.*Hændelse\s*', '') new_string

Note that this would be more efficiently done with simple string functions (although a bit more convoluted):

ltrim(substr(string, instr(string, 'Hændelse') + length('Hændelse'))) new_string 

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