Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

728 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