Solved

Oracle replace string question

Posted on 2014-01-21
6
551 Views
Last Modified: 2014-01-23
Could you please suggest how can I replace a string like the followings:

Something..bIgdOg..sMALlcaT..SomethingMore
Something..bIgdoG..sMaLlcaT..SomethingMore
Something..bIGDog..smaLLcat..SomethingMore


with the following:
Something..BigDog..SmallCat..SomethingMore

Using a oracle select statement with regex_replace or replace queries that is small (readable)? The input string has random upper and lower cases for the words BigDog and SmallCat.

Thank you.
0
Comment
Question by:toooki
6 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39798976
There is a builtin function called INITCAP that will capitalize each word:

SQL> select initcap('SmallCAT..BigDog') from dual;

INITCAP('SMALLCA
----------------
Smallcat..Bigdog

Open in new window


That's great and all, but what rule would you use to set the capital in the middle of the word since that's kinda arbitrary given the examples above
0
 

Author Comment

by:toooki
ID: 39798983
Thank you. But I cannot change the rest of the string (Something and SomethingMore and these contents are unknown. So Initcap will not work well.

I could repeatedly use the replace statement for all combination of the SmallCAT and BigDog strings with cap/small letter mix but that will look the query unreadable.
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39799078
select
     regexp_replace(
          regexp_replace(yourcolumn,'[bB][iI][gG][dD][oO][gG]','BigDog'),
          '[sS][mM][aA][lL]{2}[cC][aA][tT]','SmallCat')
from yourtable

Open in new window



or, use case insensitive search...


select
      regexp_replace(
                   regexp_replace(yourcolumn,'bigdog','BigDog',1,0,'i'),
                   'smallcat','SmallCat',1,0,'i')
from yourtable

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 32

Expert Comment

by:awking00
ID: 39800681
Without a means of identifying that a string is made up of separate words (i.e. separated by underscores or slashes or such), I don't think this can be accomplished. Except for the case, are the strings always the same or could something like rEaltInYMousE also exist?
0
 

Author Comment

by:toooki
ID: 39801883
Thank you!
The query
select
     regexp_replace(
          regexp_replace(yourcolumn,'[bB][iI][gG][dD][oO][gG]','BigDog'),
          '[sS][mM][aA][lL]{2}[cC][aA][tT]','SmallCat')
from yourtable
perfectly worked for me. Many thanks!
0
 
LVL 32

Expert Comment

by:awking00
ID: 39803014
So the part of the string that needs to be replaced is always the same (i.e. BigDog and SmallCat)?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now