Home » SQL & PL/SQL » SQL & PL/SQL » Getting only the first occurrence of the sub string in the given string (Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit ProductionE)
Getting only the first occurrence of the sub string in the given string [message #623164] Fri, 05 September 2014 07:10 Go to next message
saipradyumn
Messages: 419
Registered: October 2011
Location: Hyderabad
Senior Member
Hi All ,

I need only the first occurrence of sub string in total string by using regular expression.


Example

 select 'Revathi ,Sharath ,Cherry , Vitech, Hyd , Chandanagr, Revathi, Vitech , XYZ , Cherry, Hyd' Single_repeation
  from dual



Out should be

Revathi ,Sharath ,Cherry , Vitech, Hyd , Chandanagr, XYZ


Please help to achieve this

Thanks
Sai Pradyumn

Re: Getting only the first occurrence of the sub string in the given string [message #623168 is a reply to message #623164] Fri, 05 September 2014 08:13 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Instead of regular expression, how about coverting the row into multiple rows with each value in different row and then apply DISTINCT.
Re: Getting only the first occurrence of the sub string in the given string [message #623173 is a reply to message #623164] Fri, 05 September 2014 08:32 Go to previous messageGo to next message
saipradyumn
Messages: 419
Registered: October 2011
Location: Hyderabad
Senior Member

Hi Lalit,

This is just small part of requirement . I am just concatenating list of values by using Listagg. The list itself contains
duplicate values. My requirement is need to avoid the duplication .

Thanks
Sai Pradyumn


Re: Getting only the first occurrence of the sub string in the given string [message #623174 is a reply to message #623173] Fri, 05 September 2014 08:39 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Then catch the duplication in the list.
Re: Getting only the first occurrence of the sub string in the given string [message #623175 is a reply to message #623173] Fri, 05 September 2014 08:43 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
saipradyumn wrote on Fri, 05 September 2014 19:02

This is just small part of requirement . I am just concatenating list of values by using Listagg.


The same DISTINCT can be applied on the LISTAGG output. Please post the query if you think you need further assistance.
Re: Getting only the first occurrence of the sub string in the given string [message #623176 is a reply to message #623175] Fri, 05 September 2014 08:50 Go to previous messageGo to next message
saipradyumn
Messages: 419
Registered: October 2011
Location: Hyderabad
Senior Member

Hi Lalit ,

Distinct is not allowed in Listagg


SQL> select deptno,
  2         rtrim(listagg(distinct  ename || ', ') within group(order by deptno)) con
  3    from scott.emp
  4   group by deptno
  5  ;
 
select deptno,
       rtrim(listagg(distinct  ename || ', ') within group(order by deptno)) con
  from scott.emp
 group by deptno
 
ORA-30482: DISTINCT option not allowed for this function
 
SQL> 


Re: Getting only the first occurrence of the sub string in the given string [message #623177 is a reply to message #623176] Fri, 05 September 2014 09:07 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So apply the distinct before it gets to listagg:
select deptno,
       listagg(.....)
  from (select distinct deptno, ename from scott.emp)
Re: Getting only the first occurrence of the sub string in the given string [message #623180 is a reply to message #623177] Fri, 05 September 2014 10:14 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Or, apply distinct in the outer query. I have not tested it, but I guess it would be same as suggested by CM.
Re: Getting only the first occurrence of the sub string in the given string [message #623203 is a reply to message #623164] Fri, 05 September 2014 18:27 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
XQUERY solution:

select  xmlquery(
                 'string-join(distinct-values(ora:tokenize(.," ,"))," ,")'
                 passing 'Revathi ,Sharath ,Cherry ,Vitech ,Hyd ,Chandanagr ,Revathi ,Vitech ,XYZ ,Cherry ,Hyd'
                 returning content
                ).GetStringVal() Single_repeation
  from  dual
/

SINGLE_REPEATION
-------------------------------------------------------
Chandanagr ,Cherry ,Hyd ,Revathi ,Sharath ,Vitech ,XYZ

SQL>


SY.
Re: Getting only the first occurrence of the sub string in the given string [message #623233 is a reply to message #623180] Sat, 06 September 2014 16:54 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Lalit Kumar B wrote on Fri, 05 September 2014 16:14
Or, apply distinct in the outer query. I have not tested it, but I guess it would be same as suggested by CM.


That would mean it kicks in after listagg, which isn't the same as I suggested and wont give the OP what he wants.
Re: Getting only the first occurrence of the sub string in the given string [message #623265 is a reply to message #623233] Sun, 07 September 2014 10:54 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Correct, DISTINCT needs to be taken care before LISTAGG is applied. Else, OP would need SY's xml solution or regexp... But I would go with the distinct values fetched before applying listagg.
Re: Getting only the first occurrence of the sub string in the given string [message #623269 is a reply to message #623265] Sun, 07 September 2014 12:01 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
You could also use MODEL, recursive subquery factoring,...

SY.
Re: Getting only the first occurrence of the sub string in the given string [message #623287 is a reply to message #623269] Mon, 08 September 2014 00:29 Go to previous message
saipradyumn
Messages: 419
Registered: October 2011
Location: Hyderabad
Senior Member
Thanks Solomon Yakobson
Previous Topic: Show only one of the duplicate values ​​in a column
Next Topic: To log the row in error while selecting from database
Goto Forum:
  


Current Time: Fri Apr 26 11:22:49 CDT 2024