Home » SQL & PL/SQL » SQL & PL/SQL » Replace string based on condition (10g)
Replace string based on condition [message #643184] Thu, 01 October 2015 02:49 Go to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
I have to replace the first instance of '-' starting from left with '-RF-' .


CREATE TABLE REFAB ( REFFAB_STR VARCHAR2(60))

INSERT INTO REFAB(REFFAB_STR) VALUES ('1510029-1001');

INSERT INTO REFAB(REFFAB_STR) VALUES ('1410029M-1001');

INSERT INTO REFAB(REFFAB_STR) VALUES ('008-R230-B206');



SELECT REPLACE(REFFAB_STR,'-','-RF-') FROM REFAB;

-- I am getting the result as below

REPLACE(REFFAB_STR,'-','-RF-')
1510029-RF-1001
1410029M-RF-1001
008-RF-R230-RF-B206

--i want the result to be like below.

1510029-RF-1001
1410029M-RF-1001
008-RF-R230-B206








Re: Replace string based on condition [message #643188 is a reply to message #643184] Thu, 01 October 2015 03:00 Go to previous messageGo to next message
bugfox
Messages: 18
Registered: October 2010
Junior Member
regexp_replace(reffab_str, '-', '-RF-', 1, 1)
Re: Replace string based on condition [message #643193 is a reply to message #643188] Thu, 01 October 2015 03:08 Go to previous messageGo to next message
bugfox
Messages: 18
Registered: October 2010
Junior Member
replace(substr(reffab_str, 1, instr(reffab_str, '-')), '-', '-RF-')||substr(reffab_str, instr(reffab_str, '-') + 1)
Re: Replace string based on condition [message #643194 is a reply to message #643188] Thu, 01 October 2015 03:10 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
Thanks bugfox , can it be done without regex as i cannot use this function inside forms 6i, other way is to build a function at database and then retrieve it by passing string.
Re: Replace string based on condition [message #643195 is a reply to message #643194] Thu, 01 October 2015 03:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
See bugfox's second reply.
Re: Replace string based on condition [message #643208 is a reply to message #643195] Thu, 01 October 2015 04:20 Go to previous message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
yes , that is what i needed. thanks very much bugfox.
Previous Topic: Oracle Defined Function (DECODE)
Next Topic: SQL*Net message from dblink
Goto Forum:
  


Current Time: Thu Apr 25 01:20:12 CDT 2024