?
Solved

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

Posted on 2010-08-26
5
Medium Priority
?
1,157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 77

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.
Suggested Courses

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