sql Update query after matching with the string

I am trying to reach the query that updates a table column with the value of the other table, after that he finds an exact match.

So here's the table data and sample to create.
create table code1 
(
    codeid number,
    codedesc varchar2(60)
);

Insert into code1 values ( 1,'R1 CONTRACTS');

Insert into code1 values ( 2,'R2 CONTRACTS');

Insert into code1 values ( 3,'R3 CONTRACTS');

Insert into code1 values ( 4,'R5 CONTRACTS');

Insert into code1 values ( 5,'R9 CONTRACTS');

Insert into code1 values ( 6,'R10 CONTRACTS');

create table table1 
(   
    tablekey number,
    prefix  varchar2(25),
    codedesc    varchar2(60)
);

Insert into table1(tablekey,prefix) values (1,'1001PAC');

Insert into table1(tablekey,prefix) values (2,'1001MXT');

Insert into table1(tablekey,prefix) values (3,'1002PAE');

Insert into table1(tablekey,prefix) values (4,'1003PCS');

Insert into table1(tablekey,prefix) values (5,'1004BDX');

Insert into table1(tablekey,prefix) values (6,'1005PAC');

Insert into table1(tablekey,prefix) values (7,'1006PAC');

Insert into table1(tablekey,prefix) values (8,'1007LDR');

Insert into table1(tablekey,prefix) values (9,'1009LCR');

Insert into table1(tablekey,prefix) values (10,'1010LBR');

Insert into table1(tablekey,prefix) values (11,'ABCDEF');
I'm writing a query that would update the value of column - codedesc (currently Null) of the table1 table: after it is a string of column - table code1 codedesc.

The logic for the match is, - take - 2nd column of table-codedesc code value and get 2 characters. For example, when the string is - R1 CONTRACTS, the string will be 1. (Select substr ("R1 CONTRACTS, 2, 2) of the double). -Output will be 1.

Now,.
Look in table 1 for the 3rd position of the prefix that corresponds to the string returned by the query above. So, if the prefix is '1001PAC', it should look for 2 value of figures from the 3rd position. So, in this case it will be 01. Digitally 01 and 1 are equal, then the match is found for this line in the table1 table, so we will need to update the value of the column codedesc with the "contracts of R1.
    tablekey,    prefix            codedesc    
---------- ------------------------------------------------------------

      1               1001PAC     R1 CONTRACTS    -- Needs to be update with this value. 
      2               1001MXT     R1 CONTRACTS
      3               1002PAE      R2 CONTRACTS
...
      11             ABCDEF                                --Null ( No Need to update when no match found).
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Release 10.2.0.4.0 - 64bit Production
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Appreciate your help.

Hello

You want to avoid TO_NUMBER, given that will cause an error if even a line has a number no (except space) in the wrong place.
Use RTRIM to remove extra spaces at the end of the match_key and LPAD to add '0', if necessary, at the beginning:

MERGE INTO     table1          dst
USING   (
          SELECT  LPAD ( RTRIM ( SUBSTR ( codedesc
                                 , 2
                               , 2
                               )
                          )
                     , 2
                     , '0'
                     )          AS match_key
          ,     codedesc
          FROM     code1
     )               src
ON     (src.match_key     = SUBSTR ( dst.prefix
                       , 3
                     , 2
                     )
     )
WHEN MATCHED THEN UPDATE
     SET     dst.codedesc     = src.codedesc
;

Thanks for posting the CREATE TABLE and INSERT statements; It's very useful!

Tags: Database

Similar Questions

  • Windows update keeps reminding me with the same updates

    Windows update keeps reminding me with the same updates

    See the article after some troubleshooting for your question:

    http://support.Microsoft.com/kb/910339/en-us >

    Please let us know if it did or did not help to solve your problem.

    UTC/GMT is 22:22 Monday, may 28, 2012

  • is the new compatible update 37.0.1 with the norton toolbar? If not when?

    is the new compatible update 37.0.1 with the norton toolbar? If not when?

    According to Norton, he works with 37, https://community.norton.com/en/blogs/product-update-announcements/firefox-37-support-norton-toolbar

  • Watch is now no paired phone. The watch displays now wrong password, which must be changed on the phone.  However, given that the watch is more matched with the phone, I can't the available option to change the password.  Help please!

    I have a watch that has become not matched with the iPhone with it's Setup.  It is locked with a password and the dial of the watch indicates "Wrong password" and I have to open the App to Apple Watch on my phone to re - activate the entry of access code and try again.  However, the iphone has stated that on the phone became not paired with the watch, so I can't do anything, there is no option to change the password and I can't pair the watch with the phone because it is locked.  This is my boss's phone... Help, please!

    If you have forgotten the password for your Apple Watch - Apple...

    < edited by hosts >

  • When I do exercise machine of the staircase, the k/cal burned into the machine does not match with the Apple Watch of the activity

    When I do exercise machine of the staircase, the k/cal burned into the machine does not match with the Apple Watch of the activity

    Example of

    Stair machine 550 K/CAL

    Apple Watch 30 k/cal

    Hello

    During a workout on the upstairs computer, record your activity using the app to drive on your watch, choose the stairs like the type of activity no. This allows your watch to use sensors and the data most appropriate to evaluate the results of your activity (including your heart rate, which is measured continuously during the training, when the training app using).

    More information: use of the application of the workout on your Apple Watch - Apple Support

  • Zip code does not match with the Service request number

    Hello
    I can, check the State of repair of my Xbox to the t.
    It keeps giving the same error (ZIP Code does not match with the Service request number).

    I tried to register the product.
    but it seems impossible, if it has a status of repair open.

    a catch 22.

    Hello

    I suggest you go through the following link.

    http://www.Xbox.com/en-us/

  • my updates are not proceed with the installation even if it is powered. I've had a wndows XP SP2

    my updates are not proceed with the installation even if it is powered. I've had a wndows XP SP2

    my updates are not proceed with the installation even if it is powered. I've had a wndows XP SP2

    If you mean that Windows updates are not proceed with the installation, it is because the support of Windows XP Service Pack 2 ended on July 13, 2010, so no updates were available for this purpose since then.

    http://Windows.Microsoft.com/en-us/Windows/help/end-support

    To resolve this problem, you MUST upgrade to Windows XP Service Pack 3, which is covered and a link procided further to the bottom of the page at the link above.

    Rob

  • can I install CS5 on my laptop if 2 people already use the creative suite? I've now been referred to as forums after talking with the customer service and technical support online

    can I install CS5 on my laptop if 2 people already use the creative suite? I've now been referred to as forums after talking with the customer service and technical support online

    I was told they are separated once and also says to check the forums?

    Yes you can.

    ~ Rohit

  • After my wife update Dell 755 USSF with the latest updates I am told that my copy of windows is not genuine.

    He spent the real profit in the past and the original software that was installed with the purchase. After 3-4 years microsoft decide to play the stupid guys. Views or suggestions would be welcome. Operating systems Windows XP Pro SP3 and up-to-date with updates irritants.

    Kind regards

    Den.

    Hello there and thanks for your reply. I've sorted it by twisting the system and removing the unwanted annoyance.

  • After update of Windows 7 with the last KB 'Reliability' updates, wouldn't Boot: Boot mgr failed, cannot find the OS loader

    After that update of Windows 7 with 5 ~ updates which mainly provided increased reliability, these KBs wound up loader of Windows BOOT OS/corrupt the rupture.

    Root cause found:
    ----------------------
    Boot Manager cannot find the OS loader

    Repair action: repair of files
    Result: failed. Error code = 0xa<---- next="" repair,="" this="" changes="" to="">
    Time taken = 4206 ms

    Repair action: repair store boot configuration data
    Result: Has no = 0 error code x 490
    Time taken = 221 ms

    Repair action: System Restore
    Result: Completed successfully. Error code = 0 x 0
    Setting time = ms 44444

    How to search through my hard drive to find updates KB which is the origin of my computer to lose its bootloader/OS and not starting?  What KB update is initially the charger operating system not found when everything worked very well, before these 5 updates.

    What are the solutions?

    Try the Forum updated; It includes questions of XP, Vista and Windows 7 update:

    You will get the best help for any problem of Update/Service Pack in the Windows Update Forum; the link below:

    http://social.answers.Microsoft.com/forums/en-us/vistawu/threads

    When you repost here, kindly include the Error Codes, and exactly what is happening when you try to update.

    In this way, you will receive the best help.

    For any question on Windows 7:

    http://social.answers.Microsoft.com/forums/en-us/category/Windows7

    Link above is Windows 7 Forum for questions on Windows 7.

    Windows 7 questions should be directed to the it.

    You are in the Vista Forums.

    See you soon.

    Mick Murphy - Microsoft partner

  • I'm being invited to update Firefox, but after consultation with the details, I conclude that it can interfere with my Windows XP. How can I stop the guests?

    I always use (and will not update) Windows XP. The details of an update of Firefox indicate that the new version (that I don't need) can interfere with XP. I want to have Firefox stop invites me for an update. How can I do this? I just continue to select "Ask Me later". Is there a more permanent solution?

    Firefox 32.0.3 works great on Windows XP and contains many important security patches - actually, 'night' of pre-release Tester 35 Firefox version works fine, too.

  • Update or merge it with the return data

    Hello, I want to do an update in a single statement, and select (I want to get the data and update a field in one of the tables involved with a timestamp), I tried this:

    merge into table t

    using (SELECT ... a long and complex select...) e

    on (e.ID = t.ID)

    When matched then

    update the value t.ts = systimestamp

    e return

    It does not work because his return does not seem possible with the merger, he did the update but I get no data...

    With an update, I tried:

    Update

    (

    SELECT...) e

    Aliens set = systimestamp

    And I get ORA-01779: cannont change a column that maps to a table of key-preserver nonkey.

    The select is complex with many paintings and selects involved and more than 20 KB of text only select... and this isn't mine so I want a solution that does not need to change the internal selection if possible.

    Any ideas? Sorry for my limited knowledge of sql :-(

    No, you did not read what I said.  The RETURNING clause allows you to capture the details of the update table.  You may not have the details of the complex query that you use to determine the return lines.  There is no way in SQL to a single statement that updates a table and queries data from other tables at the same time.

    The best you're going to manage is to update the data, turning the ID (or regardless of the primary key is appropriate) and then query the query data complex where once again the ID in your collection.  However, it's really messy.  What US the real purpose of all this?

  • Windows Update error Code 800F0219 with the creative camera direct

    Original title: Code 800F0219

    This update has to do with Creative direct from the camera and the complete error code is: Creative Technology Ltd. - Streaming Media and Broadcast - Creative Live! Camera

    Download size: 285 KB

    You may have to restart your computer for this update is taken into account.

    Update type: recommended

    Creative Technology Ltd. Streaming Media and broadcast software update released in August 2009

    More information:

    http://winqual.Microsoft.com/support/?DriverID=20293226

    Help and Support:

    http://support.Microsoft.com/select/?target=hub

    In addition, there was a mistake with the number 643: Security Update for Microsoft .NET Framework 1.1 SP1 on Windows XP, Windows Vista and Windows Server 2008 x 86 (KB2742597)
    Download size: 15.3 MB
    You may have to restart your computer for this update is taken into account.
    Update type: Important
    A security issue has been identified that could allow an unauthenticated remote attacker to compromise your system and take control. You can protect your system by installing this update from Microsoft. After you install this update, you may have to restart your system.
    More information:
    http://go.Microsoft.com/fwlink/?LinkId=261907
    Help and Support:
    http://support.Microsoft.com

    Hello Arnold,.

    Thanks for posting your query in Microsoft Community.

    According to the description, you are facing problems with installation of update for Live Creative Camera and receive error code 800F0219.

    In order to quickly provide a solution, please answer these questions:

    1. is the issue with this particular update?

    2. What is the full error message?

    Follow the suggestions below for a possible solution:

    Method 1: I suggest following the steps listed in the article and check if it works.

     

    Problems with installing updates

    http://Windows.Microsoft.com/en-us/Windows-Vista/troubleshoot-problems-with-installing-updates

     

    Method 2: I suggest to perform the clean boot and then try to install the update.

    Place the computer in a clean boot state, then check if it helps. You can start Windows by using a minimal set of drivers and startup programs. This type of boot is known as a "clean boot". A clean boot helps eliminate software conflicts.

    How to perform a clean boot for a problem in Windows Vista, Windows 7 or Windows 8

    http://support.Microsoft.com/kb/929135

     

    Note: After troubleshooting, be sure to configure the computer to start as usual as mentioned in step 3 of the article mentioned above.

    Hope this information helps. For any other corresponding Windows help, do not hesitate to contact us and we will be happy to help you.

  • Update of dynamic Action with the text box

    This goes along with: update records with the text box

    APEX: 4.0.2

    THEME: SAND

    SERVER: Oracle-Application-Server-10g/10.1.3.1.0 Oracle-HTTP-Server

    I created an example with more comments on the page:

    workspace: stevendooley34

    username: dev01

    password: dev01

    application: examples of Forum

    page: update example

    I've implemented just as I think it should be put in place, but records only updates by clicking on my Refresh"" button.

    Keep in mind that the example is found in the apex 4.2.5 but I will implement this in 4.0.2.

    Thank you

    Steven



    Hi Steven,

    I think the main thing that was missing was adding the elements used in the PL/SQL "Page to go".

    The reason this is necessary is so APEX know what values of point on the browser should be sent (as part of the AJAX call) to the browser.

    IF you had all the elements that must be returned to the screen, then you will use the 'Page field items to return.

    I've also corrected a few things, P6 instead of P6 and P6_SEARCH_TYPE instead of SEARCH_TYPE, but they were just small "bugs" in the example.

    I also added another real action to refresh the report after the update.

    Thank you

    -Jorge

    PS. I do not validate that your code works for multiple, but the first glance entries and a quick test, it seems to work fine.  I focused only on the DA.

  • ring with the string value

    I would like to know if there is a way to make a single control (either a ring or other) where a string that is displayed to the user has a string in its output value.

    For example, I have a power laser with a serial port and the command to turn on the laser diodes is LASER1;

    I want to display the 'LIGHTS ON' string to the user, but that control to provide 'LASER1' value when selected.

    It's easy to do with two controls, for example, can I have an enumeration with the display of the user strings and an enum with the chains of command of the laser, but then I have to make sure any changes affect the two controls.

    If it's a single control, it is less likely to be a mistake when a change is made.

    Any help is greatly appreciated.

    I'm using LabView 2009

    Never heard of the drop-down list box?  Could be a winner, just uncheck values Match points.

Maybe you are looking for

  • camera does not turn on

    I tried to load camera after death, it does not at all, but the orange light is on when it is plugged, let it charge all night and still it won't turn. I videos I would like back, please help.

  • Xbox live windows 7

    can I get xbox live via my computer which has windows 7, like I can with windows vista?

  • Windows 7 complete retail can be installed in 32-bit or 64-bit? you have that choice?

    I don't speak of the OEM/system integrators edition that comes with a computer, I don't know the retail edition that you buy at the store. If you're not sure, do not respond. My guess is there is not a choice and there is no such thing as a 32-bit ed

  • AgileVPN does not interoperate with WLAN hostednetwork

    A mobile service on a portable application, I would like to turn on the feature of Windows 7 WLAN hostednetwork and use AgileVPN (IPSec/IKE2) during the association formed with our on-board equipment. WLAN Hostednetwork can connect to our device and

  • history of the foreground

    a method that is called when moving to the foreground background?