Home » SQL & PL/SQL » SQL & PL/SQL » Creating SQL String (Oracle 11g)
Creating SQL String [message #639890] Mon, 20 July 2015 03:43 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Hi All,

I have one string like- 'One:Two:Three'. I want to create string like- 'One','Two','Three'.

Input-
'One:Two:Three'

Expected output like-
'One','Two','Three'

Please help me out !
Re: Creating SQL String [message #639894 is a reply to message #639890] Mon, 20 July 2015 04:33 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Use quote delimiter and replace

Below URL is for your reference
http://psoug.org/reference/string_func.html


e.g. SELECT q'['One:Two:Three']' STR FROM DUAL

Thanks,
Anil MK
Re: Creating SQL String [message #639896 is a reply to message #639890] Mon, 20 July 2015 04:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> select substr(regexp_replace(replace('One:Two:Three',':','::'),
  2                               ':?([^:]+):?',',''\1'''),
  3                2) res
  4  from dual;
RES
-------------------
'One','Two','Three'

Re: Creating SQL String [message #639901 is a reply to message #639896] Mon, 20 July 2015 04:46 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thanks a lot Michel
Re: Creating SQL String [message #639951 is a reply to message #639890] Mon, 20 July 2015 10:31 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Xandot wrote on Mon, 20 July 2015 14:13

I have one string like- 'One:Two:Three'. I want to create string like- 'One','Two','Three'.


That should be a simple use of REPLACE and concatenate with single-quotation marks.

SQL> WITH DATA(str) AS(
  2  SELECT 'One:Two:Three' FROM dual
  3  )
  4  SELECT str, ''''||REPLACE(str, ':', ''',''')||'''' new_str FROM DATA;

STR           NEW_STR
------------- -----------------------------------------
One:Two:Three 'One','Two','Three'

SQL>
Re: Creating SQL String [message #639956 is a reply to message #639951] Mon, 20 July 2015 11:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Yes I wanted to give another solution than the one suggested by Anil.

Re: Creating SQL String [message #639958 is a reply to message #639894] Mon, 20 July 2015 11:38 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
anil_mk wrote on Mon, 20 July 2015 15:03


e.g. SELECT q'['One:Two:Three']' STR FROM DUAL


It seems plain and simple with hard-coded value. Sometimes it is easier to use the concatenation operator than quoting-string literal when the value is actually coming from a column/variable.

Hard-coded value:

SQL> SELECT REPLACE(q'['One:Two:Three']',':', q'[',']') str FROM DUAL;

STR
-------------------
'One','Two','Three'

SQL>


Column reference:
SQL> WITH DATA(str) AS(
  2  SELECT 'One:Two:Three' FROM dual
  3  )
  4  SELECT str, REPLACE(q'[']'||str, ':', q'[',']')||q'[']' new_str FROM DATA;

STR           NEW_STR
------------- -------------------------------------------
One:Two:Three 'One','Two','Three'

SQL> WITH DATA(str) AS(
  2  SELECT 'One:Two:Three' FROM dual
  3  )
  4  SELECT str, ''''||REPLACE(str, ':', ''',''')||'''' new_str FROM DATA;

STR           NEW_STR
------------- -----------------------------------------
One:Two:Three 'One','Two','Three'

SQL>



Re: Creating SQL String [message #640024 is a reply to message #639958] Tue, 21 July 2015 07:51 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thank you all..
Re: Creating SQL String [message #640025 is a reply to message #640024] Tue, 21 July 2015 07:52 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Why did you just create a brand newt account: "Oracle101"?

Previous Topic: Issue with Number Conversion !!
Next Topic: German Character Set
Goto Forum:
  


Current Time: Fri Apr 26 07:00:11 CDT 2024