Oracle SQL Change Last Character of String

Posted on 2011-10-11
Last Modified: 2012-05-12
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!
Question by:SCTCIT
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    in 10g and above:

    I'll work on the join.
    LVL 73

    Accepted Solution

    this should work in any version

    or you can change the SET to use the regexp above

    UPDATE tablea
       SET field1  = SUBSTR(field1, 1, LENGTH(field1 - 1)) || '7'
     WHERE acct IN (SELECT acct
                      FROM tableb
                     WHERE field2 = '200')
    LVL 73

    Expert Comment

    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
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    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');
    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


    Author Closing Comment

    THIS WORKED!!!  Thanks so much!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    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…
    Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
    Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
    This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now