?
Solved

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

Posted on 2010-08-26
5
Medium Priority
?
1,161 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 74

Accepted Solution

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

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 668 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 664 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 74

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
This video shows how to recover a database from a user managed backup
Suggested Courses

850 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