Solved

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

Posted on 2010-08-26
5
1,144 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
querying by the sum of a column in decimal 7 57
Oracle RMAN Database Restore 5 52
Best RAID for a BDD Oracle 4 62
Oracle - Create Procedure with Paramater 16 57
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…
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.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.

867 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

21 Experts available now in Live!

Get 1:1 Help Now