Operating System - HP-UX
1753672 Members
6222 Online
108799 Solutions
New Discussion юеВ

Re: Oracle - replace . with - in column

 
SOLVED
Go to solution
Ratzie
Super Advisor

Oracle - replace . with - in column

I need to update a table that in a column that anything that has a . (period) replace with - (dash)

What would be to proper sql syntax?

Update line_table
where line like '%.%'
???


Have no idea?
Thanks for the help.
4 REPLIES 4
Indira Aramandla
Honored Contributor

Re: Oracle - replace . with - in column

Hi,
To use special characters for column values you use the escape character. By default escape character is '\' ot you can define you own escape character.

To use the default escape character do this

SQL>set escape on

sql>update table_name
set column = '%\-% '
where column like '%\.%';

Here is an example I tested

SQL> select * from tab1;

NAME
----------
aaa.bbb
ccc.ddd
ttt.sss
SQL> update indira
set name = 'yyy\-nnn'
where name like '%\.%';

3 rows updated.

SQL> select * from indira;

NAME
----------
yyy-nnn
yyy-nnn
yyy-nnn

Note: That you can do this if all the column values are same with a '-'.

The other way is you can you your own special by

SQL>SQL> set escape '~'
Here '~' is you escape chanracter, so replace the '\' with '~'


Indira A
Never give up, Keep Trying
Hein van den Heuvel
Honored Contributor
Solution

Re: Oracle - replace . with - in column

Hmmm, . and - are not special characters in string constants I believe. And, the prior
example will replace each match with the same value for each row no?

I kinda suspect that the real problem was to replace just the dot with a dash and leave the reast of the colum as it was.

You'll need to sql/sqlplus string manipulation magic to make that happen.
Check out the 'string' functions in the SQL ref man, notably 'TRANSLATE' this time. But INSTR and SUBSTR are often useful.

http://h10025.www1.hp.com/ewfrf/wc/genericDocument?lc=en&cc=us&docname=c00223200

Check this out:

SQL> create table hein (test varchar(10));
SQL> insert into hein values ('aap.noot');
SQL> insert into hein values ('noot mies');
SQL> insert into hein values ('mies.teun');
SQL> select * from hein ;

TEST
----------
aap.noot
noot mies
mies.teun

SQL> update hein set test=translate(test,'.','-') where test like '%.%';

2 rows updated.

SQL> select * from hein ;

TEST
----------
aap-noot
noot mies
mies-teun

Of course the 'where like' clause for this example is kinda redundant in the sense that the same table results, because only dots will be replace. With the 'where', fewer updates are done.

hth,
Hein.



Yogeeraj_1
Honored Contributor

Re: Oracle - replace . with - in column

hi,

use of "replace" and "translate" will be a better option.

Below a more complex example:
yd@MYDB.MU> variable undesirable varchar2(255)
yd@MYDB.MU> exec :undesirable := 'SAT'

PL/SQL procedure successfully completed.

Elapsed: 00:00:05.60
yd@MYDB.MU> select username,
replace(
translate( username, :undesirable, substr(:undesirable,1,1) ),
substr(:undesirable,1,1), '' )
from all_users
where rownum < 5
/

USERNAME REPLACE(TRANSLATE(USERNAME,:UN
______________________________ ______________________________
SYS Y
SYSTEM YEM
OUTLN OULN
OPS$SWS OP$W

Elapsed: 00:00:00.01
yd@MYDB.MU>

hopw this helps!

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Ratzie
Super Advisor

Re: Oracle - replace . with - in column

Thank you!