Solved

Oracle replace string question

Posted on 2014-01-21
6
557 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 61
Connecting to multiple databases to create a Dashboard 5 26
Oracle Insert not working 10 28
Email query results in HTML 6 20
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to recover a database from a user managed backup

777 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