Home » SQL & PL/SQL » SQL & PL/SQL » replace a letter ' (Oracle 11g)
replace a letter ' [message #625456] Wed, 08 October 2014 06:20 Go to next message
rohan10k
Messages: 15
Registered: May 2011
Junior Member
Hi All,

I am using Oracle 11g version.
I want to convert strings as below please help me.

(1)Original string is as below

('Singapore ,New York , Ind'ia')

It has to convert as below

('Singapore,New York, Ind''ia')



(2) Original string is

('One F'ive, Two ,Three ')

It should convert as below

('One F''ive,Two,Three')


Thanks in advance.
Re: replace a letter ' [message #625458 is a reply to message #625456] Wed, 08 October 2014 06:30 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
replace is your friend, with optional chr:
SQL> 
SQL> WITH DATA AS (SELECT 'One F''ive,Two,Three' a FROM dual
  2                UNION ALL
  3                SELECT 'Singapore ,New York , Ind''ia' a FROM dual
  4               )
  5  SELECT a, REPLACE(a, '''', '''''') conv_a, REPLACE(a, CHR(39), CHR(39)||CHR(39)) conv2_a
  6  FROM DATA;
 
A                            CONV_A                            CONV2_A
---------------------------- --------------------------------- --------------------------------
One F'ive,Two,Three          One F''ive,Two,Three              One F''ive,Two,Three
Singapore ,New York , Ind'ia Singapore ,New York , Ind''ia     Singapore ,New York , Ind''ia
Re: replace a letter ' [message #625462 is a reply to message #625458] Wed, 08 October 2014 06:47 Go to previous messageGo to next message
rohan10k
Messages: 15
Registered: May 2011
Junior Member
Thank you very much for the reply.

The string contains spaces that need to be removed. Please consider the spaces in the string.

Example :
string=('Singapore , New York, Ind'ia')

output has to be ('Singapore,New York, Ind''ia')
Re: replace a letter ' [message #625463 is a reply to message #625462] Wed, 08 October 2014 06:58 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
so use replace to remove them
Re: replace a letter ' [message #625464 is a reply to message #625462] Wed, 08 October 2014 06:59 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
SQL> with data as(
  2  select q'['Singapore , New York, Ind'ia']' a from dual union all
  3  select q'['One F'ive, Two ,Three']'  from dual)
  4  SELECT A,
  5  REPLACE(REPLACE(REPLACE(A, '''', ''''''), ' ,',','),', ',',') conv_a,
  6  REPLACE(REPLACE(REPLACE(A, CHR(39), CHR(39)||CHR(39)), ' ,',','),', ',',') conv2_a
  7  FROM DATA
  8  /

A                                             CONV_A                                        CONV2_A
--------------------------------------------- --------------------------------------------- ---------------------------------------------
'Singapore , New York, Ind'ia'                ''Singapore,New York,Ind''ia''                ''Singapore,New York,Ind''ia''
'One F'ive, Two ,Three'                       ''One F''ive,Two,Three''                      ''One F''ive,Two,Three''

SQL>

[Updated on: Wed, 08 October 2014 07:03]

Report message to a moderator

Re: replace a letter ' [message #625466 is a reply to message #625464] Wed, 08 October 2014 07:17 Go to previous messageGo to next message
rohan10k
Messages: 15
Registered: May 2011
Junior Member
Thanks, but the result is not getting as expected.

Please consider the string including the brackets as below.

('Singapore ,New York , Ind'ia')

the output has to be as below

('Singapore,New York,Ind''ia')


and the spaces in between comma are not constant, may be 5 spaces or 15 spaces like
('Singapore<5 spaces>,New York<15 spaces>,<2 spaces> Ind'ia')
Re: replace a letter ' [message #625470 is a reply to message #625466] Wed, 08 October 2014 08:14 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
And after the answer is given to that, what new restrictions will you bring up?

Why not give all the rules up front to avoid this game?
Re: replace a letter ' [message #625474 is a reply to message #625470] Wed, 08 October 2014 08:41 Go to previous messageGo to next message
rohan10k
Messages: 15
Registered: May 2011
Junior Member
joy_division, in my first query itself its clear. The input string is having brackets and the spaces it is displaying single space eventhoug i given 5 spaces.
Re: replace a letter ' [message #625476 is a reply to message #625474] Wed, 08 October 2014 08:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: replace a letter ' [message #625479 is a reply to message #625476] Wed, 08 October 2014 09:17 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@OP, we have given you more than a start sort of query. Even though you didn't show us what you tried yourself, you been provided complete answer based on your original requirements. Now it is the time for you to SHOW us what you have tried. Be professional. Because, this is NOT a spoon-feeding/homework asssignment forum. Hope you understand and follow the guidelines. Help us and get helped!
Previous Topic: FORALL BULK COLLECT
Next Topic: PL/SQL home test
Goto Forum:
  


Current Time: Fri Apr 26 16:30:11 CDT 2024