Solved

Oracle replace string question

Posted on 2014-01-21
6
539 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 31

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 31

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

746 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

12 Experts available now in Live!

Get 1:1 Help Now