SCTCIT
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
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]
Any help would be greatly appreciated! THANKS!
Terry
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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;
ASKER
THIS WORKED!!! Thanks so much!
Terry
Terry
regexp_replace(column_name
I'll work on the join.