Luftwaffe Luftwaffe - 10 months ago 47
SQL Question

Oracle: How to use regexp_substr in this case

I have a table in Oracle where one of the column contains UserIds which are in the form of \. For eg "fin\george", "sales\andy" etc. How can I use REGEXP_SUBSTR function to get only the from the UserIds. ie I want to fetch only "george", "andy" etc. I have achieved the desired reult using SUBSTR function but I want to use REGEXP_SUBSTR in this case.

I tried doing this:

SELECT REGEXP_SUBSTR('fin\george','\[^\]+,') "UserName" FROM DUAL;

but it did'nt help. Can anyone please point out my mistake ?

Answer Source

I believe you want to use a regexp_replace with a backreference. I'm assuming that all the characters before and after the \ are alphabetic. If you allow numbers, you'd want to use the [[:alnum:]] rather than [[:alpha::].

  1* SELECT REGEXP_replace('fin\george',
                           '\2') "UserName" 
       FROM DUAL
SQL> /