Solved

How to shorten, trim, lowercase, remove non-alphanumeric characters and update all at once

Posted on 2010-08-26
5
1,143 Views
Last Modified: 2013-12-07
Hello,

I'm a newbie at SQL and PL/SQL, so please bear with me...

I have 2 tables, both with multiple columns. In table A there is a column named NAME and in table B there is a column named SHORTNAME. Both of those columns are VARCHAR2(254 BYTE) and both tables have an ID column so I can join them up.

What I want to do is, where a.ID = b.ID, I want to take the value of a.NAME, remove any spaces (before, after and in between), change any chars to lower case, remove any characters that aren't  a to z or 1 to 0, and "trim" the length to be a maximum of 10 alpha numeric characters. And finally, take the result and update it into b.SHORTNAME. Yes, a.NAME can be multiple words and it can include numbers and possibly other characters. I only want letters and/or numbers.

Here is an example result...
a.NAME = Experts Exchange
b.SHORTNAME=expertsexc

OR:
a.NAME=Hey! You!
b.SHORTNAME=heyyou

There are about 2k rows that I need to update, so I'm assuming that this would be done in PL/SQL? I don't know, I've never written anything this complicated in PL/SQL.

I hope I was clear enough. If not, please let me know.

Thanks,
biglarrrr
0
Comment
Question by:biglarrrr
5 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 167 total points
ID: 33535293
update yourtable set shortname = regexp_replace(lower(name),'[^a-z0-9]')
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 167 total points
ID: 33535386
missed: length to be a maximum of 10 alpha numeric characters

... substr(regexp_replace(lower(name),'[^a-z0-9]'),1,10)
0
 
LVL 14

Assisted Solution

by:ajexpert
ajexpert earned 166 total points
ID: 33535394
just to add that biglarrrr needs only 10 characters as short name
 
 

update yourtable set shortname = SUBSTR(regexp_replace(lower(name),'[^a-z0-9]')), 1, 10)

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 33535425
oops, thanks for the addition
0
 

Author Comment

by:biglarrrr
ID: 33536871
Thanks everyone. Below is the PL/SQL I came up with and it worked!

Thanks,
biglarrrr
begin



for rec in ( select id,name from tablea where name IS NOT NULL )

loop



update tableb

set shortname = substr(regexp_replace(lower(rec.name),'[^a-z0-9]'),1,10)

where id = rec.id;



end loop;



end;

Open in new window

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.

Join & Write a Comment

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
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.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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…

747 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