Search code examples
sqlregexoracle-databaseregexp-replace

How to gather data left of a string of text in Oracle SQL


I have a SQL that is used to return all customer data, however, it is all in one field. The SQL I am using is:

SELECT Cust_Desc
FROM All_Cust_Data

Cust_Desc returns the following information in the following format

John, Doe, Client ID 7, Region Code, 4....

I need to split the data left of everything from , Region Code so the query returns only John, Doe, Client ID 7.


Solution

  • use regexp_replace

    with All_Cust_Data(Cust_Desc) as
    (
     select 'John, Doe, Client ID 7, Region Code, 4....' from dual
    )
    select regexp_replace(Cust_Desc,'(.*),.\Region Code.*','\1') as "Result String"
      from All_Cust_Data;
    
        Result String
    -----------------------
    John, Doe, Client ID 7
    

    Rextester Demo