Home » SQL & PL/SQL » SQL & PL/SQL » Remove Second Occurrence of a word in String (oracle form 10G)
Remove Second Occurrence of a word in String [message #642161] Fri, 04 September 2015 04:09 Go to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
This can be very stupid question but how to remove second occurrence of a word in a string.
In Oracle Form, I am creating a string & storing in display field based on the data block values. Message has lot of repetitive words.
Sample string is :

tmp_msg := SP10/XYZ(F NIL)SP10/XYZ(L NIL)SP1(F NIL)SP1(L NIL)SP1(S NIL)

In above string, want to remove second occurrence of SP10/11A and SP1. Tried lot of instr, replace, substr but unable to get it right.

Please help.

Re: Remove Second Occurrence of a word in String [message #642162 is a reply to message #642161] Fri, 04 September 2015 04:13 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
There's no SP10/11A in TMP_MSG. Perhaps you should provide a valid test case.
Re: Remove Second Occurrence of a word in String [message #642163 is a reply to message #642162] Fri, 04 September 2015 04:16 Go to previous messageGo to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
Sorry its SP10/XYZ,
Re: Remove Second Occurrence of a word in String [message #642164 is a reply to message #642163] Fri, 04 September 2015 04:31 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
There are 3 occurrences of SP1 that string. Which one(s) do you want to remove? Just one? Two of them? What would the final result look like?

Are "SP10/XYZ" and "SP1" fixed and you'll want to remove just these strings, or are there different TMP_MSG values (such as XV1(1)XV1(2)) so you'd want to remove the second "XV1" which, actually, means that you'd first have to analyze TMP_MSG, find out which strings are duplicates, and then remove one of them? If that's so, what are the rules that make some string "a duplicate"? In your TMP_MSG, "S" itself is duplicated, or "10", or "NIL".

BTW, I'm moving your question into our (PL/)SQL forum as the problem isn't strictly Forms related.
Re: Remove Second Occurrence of a word in String [message #642165 is a reply to message #642164] Fri, 04 September 2015 04:39 Go to previous messageGo to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
The input will be like ( This is sample string, it is not fixed in lengthwise, it can vary with repeating words.

tmp_msg := SP10/XYZ(F NIL)SP10/XYZ(L NIL)SP1(F NIL)SP1(L NIL)SP1(S NIL)

Output should be like :
SP10/XYZ(F NIL)(L NIL)SP1(F NIL)(L NIL)(S NIL)

i.e. second occurrence of SP10/XYZ and SP1. I don't want to move character like repeating S, 1 or P. a complete word to be removed.
Re: Remove Second Occurrence of a word in String [message #642169 is a reply to message #642165] Fri, 04 September 2015 05:58 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
Try below link...u'll get some idea

http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
Re: Remove Second Occurrence of a word in String [message #642172 is a reply to message #642169] Fri, 04 September 2015 08:15 Go to previous messageGo to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
Thanks Aspire But Regex_replace is not supporting.
Re: Remove Second Occurrence of a word in String [message #642179 is a reply to message #642172] Fri, 04 September 2015 09:06 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
malya wrote on Fri, 04 September 2015 08:15
Thanks Aspire But Regex_replace is not supporting.


What is your oracle version?

Re: Remove Second Occurrence of a word in String [message #642180 is a reply to message #642179] Fri, 04 September 2015 09:07 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
sandeep_orafaq wrote on Fri, 04 September 2015 07:06
malya wrote on Fri, 04 September 2015 08:15
Thanks Aspire But Regex_replace is not supporting.


What is your oracle version?


scroll to the TOP

Remove Second Occurrence of a word in String (oracle form 10G)
Re: Remove Second Occurrence of a word in String [message #642181 is a reply to message #642180] Fri, 04 September 2015 09:37 Go to previous messageGo to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
Database 10G and Form & Reports 10G. I tried with REGEX_REPLACE but did not work.
Re: Remove Second Occurrence of a word in String [message #642182 is a reply to message #642181] Fri, 04 September 2015 09:40 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I tried with REGEX_REPLACE but did not work.
"did not work" is 100% devoid of any actionable detail.
Please use COPY & PASTE to show us exactly what you did & how Oracle responded.

Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read

Re: Remove Second Occurrence of a word in String [message #642183 is a reply to message #642182] Fri, 04 September 2015 09:42 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
post sample code

[Updated on: Fri, 04 September 2015 09:46]

Report message to a moderator

Re: Remove Second Occurrence of a word in String [message #642185 is a reply to message #642183] Fri, 04 September 2015 10:07 Go to previous messageGo to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
its Form Code on a button (when button press trigger)-
-------------
loop on data block;

Data Block Appear like this

ProjectCode   Product  Figures Remarks
-----------   -------  ------- -------
SP10          LENOVO       853     NA
SP10          DELL         980     NOSP
SP10          INTEL         90     HBH
SP10/11A      BOSS         180     ING
SP10/11A      ORAFAQ       777     VHJ



Now I want to create message like below (Desired Output)

tmp_msg = SP10(LENOVO/853/NA, DELL/980/NOSP, INTEL/90/HBH)SP10/11A(BOSS/180/ING,ORAFAQ/777/VHJ)

Declare
tmp_msg VARCHAR2(1000);
LOOP
IF instr(tmp_msg,:project,1,1) <> 0 THEN
:project ='';
END IF;

tmp_msg := Project||'('||product||'/'||figures||'/'||remarks;
END Loop;


With above code, I am able to disappear second & third occurrence of SP10 but SP10/11A has SP10 so SP10/11A is not displaying for first time.


[mod-edit: code tags added by bb]

[Updated on: Fri, 04 September 2015 16:15] by Moderator

Report message to a moderator

Re: Remove Second Occurrence of a word in String [message #642186 is a reply to message #642185] Fri, 04 September 2015 10:13 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
why do you purposefully ignore Posting Guidelines you were asked to follow?
Re: Remove Second Occurrence of a word in String [message #642187 is a reply to message #642185] Fri, 04 September 2015 10:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And where is the part:

malya wrote on Fri, 04 September 2015 16:37
Database 10G and Form & Reports 10G. I tried with REGEX_REPLACE but did not work.


Re: Remove Second Occurrence of a word in String [message #642199 is a reply to message #642187] Fri, 04 September 2015 15:25 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Also, you should not use a string to hold multiple pieces of information. They should all be in a daughter table or array.
Re: Remove Second Occurrence of a word in String [message #642200 is a reply to message #642185] Fri, 04 September 2015 18:52 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
You should be able to adapt the following to your form situation.

SCOTT@orcl> SELECT * FROM data_block
  2  /

PROJECTCODE PRODUCT    FIGURES REMARKS
----------- ------- ---------- -------
SP10        LENOVO         853 NA
SP10        DELL           980 NOSP
SP10        INTEL           90 HBH
SP10/11A    BOSS           180 ING
SP10/11A    ORAFAQ         777 VHJ

5 rows selected.

SCOTT@orcl> DECLARE
  2    tmp_projectcode	VARCHAR2(1000) := '';
  3    tmp_msg		VARCHAR2(1000) := '';
  4  BEGIN
  5    FOR r IN (SELECT * FROM data_block ORDER BY projectcode)
  6    LOOP
  7  	 IF tmp_projectcode IS NULL THEN
  8  	   tmp_msg := tmp_msg || r.projectcode || '(';
  9  	   tmp_projectcode := r.projectcode;
 10  	 ELSIF r.projectcode != tmp_projectcode THEN
 11  	   tmp_msg := RTRIM (tmp_msg, ', ') || ')' || r.projectcode || '(';
 12  	   tmp_projectcode := r.projectcode;
 13  	 END IF;
 14  	 tmp_msg := tmp_msg || r.product || '/' || r.figures || '/' || r.remarks || ', ';
 15    END LOOP;
 16    tmp_msg := RTRIM (tmp_msg, ', ') || ')';
 17    DBMS_OUTPUT.PUT_LINE (tmp_msg);
 18  END;
 19  /
SP10(LENOVO/853/NA, DELL/980/NOSP, INTEL/90/HBH)SP10/11A(BOSS/180/ING, ORAFAQ/777/VHJ)

PL/SQL procedure successfully completed.

Re: Remove Second Occurrence of a word in String [message #642201 is a reply to message #642200] Sat, 05 September 2015 00:45 Go to previous messageGo to next message
malya
Messages: 22
Registered: August 2015
Location: Hyderabad, India
Junior Member
Hi Barbara !!
Thanks a lot for the logic. It worked. Smile
Re: Remove Second Occurrence of a word in String [message #642205 is a reply to message #642201] Sat, 05 September 2015 07:15 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
SQL> select  *
  2    from v$version
  3  /

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for 64-bit Windows: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> select  xmlagg(
  2                 xmlelement(
  3                            e,
  4                            projectcode || '(' ||
  5                              rtrim(
  6                                    xmlagg(
  7                                           xmlelement(
  8                                                      e,
  9                                                      product || '/' || figures || '/' || remarks,
 10                                                      ', '
 11                                                     ).extract('//text()')
 12                                         ),
 13                                    ', '
 14                                   ) || ')'
 15                           ).extract('//text()')
 16                ) str
 17    from  data_block
 18    group by projectcode
 19  /

STR
------------------------------------------------------------------------------------------------------
SP10(LENOVO/853/NA, DELL/980/NOSP, INTEL/90/HBH)SP10/11A(BOSS/180/ING, ORAFAQ/777/VHJ)

SQL> 


SY.
Previous Topic: ora-00900 invalid sql statement while using open cursor for variable
Next Topic: PLSQL - providing values to Bind variables appearing multiple times in USING clause
Goto Forum:
  


Current Time: Thu Apr 25 15:21:20 CDT 2024