Home » SQL & PL/SQL » SQL & PL/SQL » Creating String (Oracle 11g)
Creating String [message #640596] Fri, 31 July 2015 08:25 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hi All,

I have one query and I want to modify string like below output:

I/P Query:

select 'Production No: PO_P12334, PO_P23234, 23232:2, 23323:4' str from dual

Output Like:

Str
------
PO_P12334;PO_P23234;23232:2;23323:4;



Thanks,
Xandot


[MERGED by LF]

[Updated on: Fri, 21 August 2015 01:02] by Moderator

Report message to a moderator

Re: Creating String [message #640597 is a reply to message #640596] Fri, 31 July 2015 08:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT 'PO_P12334;PO_P23234;23232:2;23323:4' FROM DUAL;
Re: Creating String [message #640598 is a reply to message #640597] Fri, 31 July 2015 08:29 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thanks For update BlackSwan
Re: Creating String [message #640599 is a reply to message #640598] Fri, 31 July 2015 08:32 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
But My String like :

select 'Production No: PO_P12334, PO_P23234, 23232:2, 23323:4' str from dual;
Re: Creating String [message #640602 is a reply to message #640599] Fri, 31 July 2015 08:37 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Then explain the rules to achieve your desired output.
Re: Creating String [message #640604 is a reply to message #640602] Fri, 31 July 2015 08:45 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
String : 'Production No: PO_P12334, PO_P23234, 23232:2, 23323:4'

Rules : 1- No Spaces between comma
2- comma should be replace by ';'
3- final output should be like : PO_P12334;PO_P23234;23232:2;23323:4;
Re: Creating String [message #640605 is a reply to message #640604] Fri, 31 July 2015 08:54 Go to previous messageGo to next message
CraigB
Messages: 386
Registered: August 2014
Location: Utah, USA
Senior Member
Take a look at the REPLACE() Oracle Function.
Re: Creating String [message #640606 is a reply to message #640605] Fri, 31 July 2015 09:44 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
I'm trying but spaces are still there:

select replace(substr('Production No: PO_P12334, PO_P23234, 23232:2, 23323:4', instr('Production No: PO_P12334, PO_P23234, 23232:2, 23323:4',':')+2),',',';')||';' from dual;

Please help me out.
Re: Creating String [message #640607 is a reply to message #640606] Fri, 31 July 2015 09:50 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Xandot wrote on Fri, 31 July 2015 07:44
I'm trying but spaces are still there:

select replace(substr('Production No: PO_P12334, PO_P23234, 23232:2, 23323:4', instr('Production No: PO_P12334, PO_P23234, 23232:2, 23323:4',':')+2),',',';')||';' from dual;

Please help me out.


when all else fails, Read The Fine Manual

https://docs.oracle.com/database/121/SQLRF/functions166.htm#sthref1829
Re: Creating String [message #640608 is a reply to message #640607] Fri, 31 July 2015 10:22 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Still I'm not getting output. Please help me out
Re: Creating String [message #640612 is a reply to message #640608] Fri, 31 July 2015 10:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Here's the first step to get the result which is not in your rules:
SQL> with data as (select 'Production No: PO_P12334, PO_P23234, 23232:2, 23323:4' str from dual)
  2  select substr(str,instr(str,' ',1,2)+1) from data;
SUBSTR(STR,INSTR(STR,'',1,2)+1)
--------------------------------------
PO_P12334, PO_P23234, 23232:2, 23323:4

[Updated on: Fri, 31 July 2015 10:45]

Report message to a moderator

Re: Creating String [message #640619 is a reply to message #640608] Fri, 31 July 2015 11:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Xandot wrote on Fri, 31 July 2015 08:22
Still I'm not getting output. Please help me out


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: Creating String [message #641637 is a reply to message #640596] Wed, 19 August 2015 08:55 Go to previous messageGo to next message
ramsql
Messages: 4
Registered: August 2015
Location: chennai
Junior Member
This might help

with t as
(select 'Production No: PO_P12334, PO_P23234, 23232:2, 23323:4' str from dual)
select substr(replace(str,',',';'),16) from t;
Re: Creating String [message #641652 is a reply to message #641637] Wed, 19 August 2015 14:57 Go to previous messageGo to next message
shahbazali.001
Messages: 1
Registered: December 2014
Location: Pakistan
Junior Member

i think it will work.......
select replace(substr('Production No: PO_P12334, PO_P23234, 23232:2, 23323:4',
instr('Production No: PO_P12334, PO_P23234, 23232:2, 23323:4',':')+2),', ',';') from dual
Re: Creating String [message #641658 is a reply to message #641652] Thu, 20 August 2015 01:12 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thank you all..
Creating String [message #641672 is a reply to message #640596] Thu, 20 August 2015 03:21 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hi All,

I have string like:

select 'PB_T12345,PB_T67890,123434:2,657657:3,PB_T23233' from dual;


I want output like:
123434:2,657657:3


String should not contain any value which has 'PB_T', Please help me out.


Thanks,
Xandot
Re: Creating String [message #641675 is a reply to message #641672] Thu, 20 August 2015 03:38 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
1) Split string as Lalit showed you in your last thread
2) Remove values starting with PB
3) Recombine
Re: Creating String [message #641677 is a reply to message #641672] Thu, 20 August 2015 03:40 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I think you should try to first learn from so many solutions already provided to you. All your questions are similar to each other.

SQL> WITH t(str) AS(
  2  SELECT 'PB_T12345,PB_T67890,123434:2,657657:3,PB_T23233' FROM dual
  3  )
  4  SELECT listagg(str, ',') within GROUP(
  5  ORDER BY id) str
  6  FROM
  7    (SELECT level id,
  8      trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
  9    FROM t
 10      CONNECT BY LEVEL <= regexp_count(str, ',')+1
 11    )
 12  WHERE instr(str, 'PB_T') = 0;

STR
-----------------------------------------------------------------------
123434:2,657657:3

SQL>
Re: Creating String [message #641679 is a reply to message #641677] Thu, 20 August 2015 03:43 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Lalit Kumar B wrote on Thu, 20 August 2015 09:40
I think you should try to first learn from so many solutions already provided to you.


You rather undermined that sentence by posting the code don't you think?
Re: Creating String [message #641680 is a reply to message #641679] Thu, 20 August 2015 03:47 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Hmm...I should have seen all his previous questions, I just saw those which came up today. Now I see almost all his previous questions revolving around similar topic.
Re: Creating String [message #641683 is a reply to message #641680] Thu, 20 August 2015 04:03 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thanks Lalit.. I apologies for repetitive threads..

select LISTAGG(cp, ',')WITHIN GROUP (ORDER BY NULL) from 
(select regexp_substr('PB_T12345,PB_T67890,123434:2,657657:3,PB_T23233','[^,]+', 1, level) as cp from dual        
           connect by regexp_substr('PB_T12345,PB_T67890,123434:2,657657:3,PB_T23233', '[^,]+', 1, level) is not null)
where instr(cp, 'PB_T')=0;      
Re: Creating String [message #641684 is a reply to message #641683] Thu, 20 August 2015 04:16 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
If you care about order then use the query I provided, else order is not guaranteed.
Previous Topic: PL/SQL: ORA-00911: invalid character
Next Topic: Returning a sys_refcursor from an explicit cursor
Goto Forum:
  


Current Time: Fri Apr 19 05:01:55 CDT 2024