Link to home
Start Free TrialLog in
Avatar of SCTCIT
SCTCITFlag for United States of America

asked on

Oracle SQL Change Last Character of String

I usually work in MS SQL, but in Oracle, I need to change the last character of a value in a text field in TableA to a '7' if a value in field2 in TableB is '200'.  (Both tables have the acct field.)  Here is my code:

UPDATE (SELECT A.field1, B.field2
               FROM TableA A, TableB B
             WHERE A.acct=B.acct and B.field2 = '200')
        
SET A.field1 = left([field1],len([field1])-1) & '7';

Any help would be greatly appreciated!   THANKS!
Terry
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

in 10g and above:
regexp_replace(column_name,'.$','7')

I'll work on the join.
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually, there is a slight difference.


the regexp version will update NULL to be NULL
mine will update NULL to be '7'

both should work for all populated strings though.

if you want to skip NULL's then, rather than modifying the SET,  add that to the WHERE clause

 WHERE acct IN (SELECT acct
                  FROM tableb
                 WHERE field2 = '200')
AND field1 IS NOT NULL
sdstuber posted the full update using IN.

Here is what I was going for.  Same rules apply for nulls pointed out by sdstuber.
drop table tab1 purge;
create table tab1(acct number primary key, field1 varchar2(100));

drop table tab2 purge;
create table tab2(acct number primary key, field2 varchar2(100));

insert into tab1 values(1,'abcdef');
insert into tab1 values(2,'abcdef');
insert into tab2 values(2,'200');
commit;



update (select field1 
   from tab1 t1 
   inner join tab2 t2 on t1.acct = t2.acct 
   where t2.field2 = '200') t
   set t.field1=regexp_replace(field1,'.$','7');

select * from tab1;

Open in new window

Avatar of SCTCIT

ASKER

THIS WORKED!!!  Thanks so much!
Terry