Need logic to update Varchar column in a table

Hi,
Could you give me logic for below.
I have a table xx_dbc where one of the column Attribute1 is varchar2(240).
Attribute1 has values like 
==================
(AIN,PUP,GRI,NINE)
(AIN,PUP)
(AIN)
(GRI,NINE)
I have to update this column for every record with values AIN=10 , PUP=20 , GRI=30 , NINE=40
Columns should be updated like below
(10,20,30,40)
(10,20)
(10)
(30,40)
Thanks.

Hi,
Here's one way:
WITH  replacements  AS
    SELECT  'AIN' AS old_str, '10' AS new_str  FROM dual  UNION ALL
    SELECT  'PUP',            '20'             FROM dual  UNION ALL
    SELECT  'GRI',            '30'             FROM dual  UNION ALL
    SELECT  'NINE',           '40'             FROM dual
,    normalized_data  AS
    SELECT  p_key
    ,       LEVEL               AS sort_key
    ,       REGEXP_SUBSTR ( attribute1
                          , '[^(,)]+'
                          , 1
                          , LEVEL
                          )     AS str
    FROM    xx_dbc
--  WHERE   ...     -- If you need any filtering, put it here
    CONNECT BY   LEVEL              <= 1 + REGEXP_COUNT (attribute1, ',')
            AND  PRIOR p_key        = p_key
            AND  PRIOR SYS_GUID ()  IS NOT NULL
SELECT    '(' || LISTAGG ( NVL (r.new_str, n.str)
                         ) WITHIN GROUP (ORDER BY sort_key)
              || ')'  AS  new_attribute1
,         n.p_key                         -- If wanted
FROM             normalized_data  n
LEFT OUTER JOIN  replacements     r  ON  r.old_str  = n.str
GROUP BY  n.p_key
ORDER BY  n.p_key
Like everything else, it depends on your Oracle version.
In the query above, p_key can be any unique key from xx_dbc, including attribute1 or ROWID.
Relational databases, like Oracle, work best when each column of each row contains only 1 piece of information, not a delimited list of any number of pieces.  This is so fundamental to table design that it's called First Normal Form.  Most of the work in this problem involves converting your denormalized data into First Normal Form, and then converting it back again.  This problem, like many others, would be much simpler and more efficient if your table was in First Normal Form.
The query above assumes the replacement strings (such as 'AIN' and '10') are not already in a table.  If they are, or if they can be derived from a table, then you don't need the replacements sub-query; use your real table instead.

Similar Messages

  • How to update two columns in two tables?

    hi friends
    I have two tables linked to each other through SaleNo and SaleDT. their structure as below
    Sales Table============
    SaleNo int PK auto increment
    SaleDT Datetime PK
    Qnty decimal
    Units decimal
    Invoices table
    =================InvoiceNo int PK Auto incremented
    InvoiceDT Datetime
    SaleNo int FK
    SaleDT Datetime FK
    Note that SaleDT column is NOT assigned with getDate() expression.
    1. what I need to do is update the SaleDT column of Sales table and Invoices table with the value '2013-01-31 10:31:55.813', how do I do this without manually breaking the link between the tables?
    2. Assume SaleNos 15 to 27 needs update the SaleDT to '2013-06-12 10:31:55.813', how do I do this complex operation where I I have update SaleDt column of two table of SaleNo range from 15 to 27?
    thanks
    I use Visual studio 2012 Ultimate and SQL server 2008 developer edition!

    Why not below? May be you will not be able to change your design now, but just want to share.
    Sales Table============
    SALEID int PK autoincrement
    SaleNo int
    SaleDT Datetime
    Qnty decimal
    Units decimalUNIQUE (Saleno,SaleDT)
    Invoices table
    =================InvoiceNo int PK Auto incremented
    InvoiceDT Datetime
    SALEID int FK

  • How to update multiple columns from different tables using cursor.

    Hi,
    i have two tables test1 and test2. i want to udpate the column(DEPT_DSCR) of both the tables TEST1 and TEST2 using select for update and current of...using cursor.
    I have a code written as follows :
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
         LOOP
              FETCH C1 INTO v_mydept1,v_mydept2;
              EXIT WHEN C1%NOTFOUND;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
              UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
         END LOOP;
         COMMIT;
    END;
    The above code when run says that it runs successfully. But it does not updates the desired columns[DEPT_DSCR].
    It only works when we want to update single or multiple columns of same table...i.e. by providing these columns after "FOR UPDATE OF"
    I am not sure what is the exact problem when we want to update multiple columns of different tables.
    Can anyone help me on this ?

    oops my mistake.....typo mistake...it should have been as follows --
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    Now here is the upated PL/SQL code where we are trying to update columns of different tables --
    DECLARE
    v_mydept1 TEST1.DEPT_CD%TYPE;
    v_mydept2 TEST2.DEPT_CD%TYPE;
    CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
    BEGIN
    OPEN C1;
    LOOP
    FETCH C1 INTO v_mydept1,v_mydept2;
    EXIT WHEN C1%NOTFOUND;
    UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
    UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
    END LOOP;
    COMMIT;
    END;
    Please let us know why it is not updating by using using CURRENT OF

  • Need help in updating "TIMESTAMP" column

    Hi,
    Below is my table description, i want to update timestamp column but i could not able to do, please help me on this.
    ID VARCHAR2(4 BYTE),
    TYPE VARCHAR2(1 BYTE),
    DATET TIMESTAMP(6),
    TYPE NUMBER
    Currently i have value "5/1/2009 9:00:00.000000 AM" for DATET column, now i want to update it to "5/1/2009 9:00:00.000000 PM". Please help me out.
    Thanks.

    Hi,
    Here's one way:
    UPDATE     table_x
    SET     datet     = datet + INTERVAL '12' HOUR
    WHERE     datet     = TO_TIMESTAMP ( '01/05/2009 09:00:00'
                          , 'DD/MM/YYYY HH24:MI:SS'     -- or whatever
                          );

  • Update 1 column,1 single table based on where results of multiple tables

    I would like the my_id column in 1 table updated to the static value of 247 for my_id stored in the 1 single table based on the where clause, which uses the my_id column in it.
    The update statement updates all rows in table1, instead of just the rows where
    the condition ( (b.my_id=a.my_id)
    and ( b.my_name like 'OIS SrClerk%')
    and (a.f_id=m.f_id)
    and (trunc(m.cr_time) < '02-Apr-2008')) is true
    What needs to be changed?
    update table1 a
    set a.my_id=247
    from table1 a, table2 b, table3 m
    where
    (b.my_id=a.my_id)
    and ( b.my_name like 'OIS SrClerk%')
    and (a.f_id=m.f_id)
    and (trunc(m.cr_time) < '02-Apr-2008')

    Are you looking for this?
    UPDATE table1 A
       SET A.my_id = 247
    WHERE EXISTS (SELECT 'x'
                     FROM table2 b,
                          table3 M
                    WHERE b.my_id = A.my_id
                     AND b.my_name LIKE 'OIS SrClerk%'
                     AND A.f_id = M.f_id
                     AND TRUNC (M.cr_time) < to_date('02-apr-2008 00:00:00','dd-mon-yyyy HH:MI:SS') )changed date string to to_date
    Message was edited by:
    devmiral

  • Question about creating and updating new columns in a table

    Hi, I have a couple of questions that would love some answers to
    Please bear in min that questions relate to datawarehouse / Just large tables with say about 50 million records
    1) How do you create new columns in a big table
    2) how do you update the new columns
    3) would you create an index on a column in a table with 200m rows when the only two possible values are stored in the column (e.g.Y, N) - give your resason
    my aswers to these would be
    1) simple alter table ...
    2) simple update statements
    3) No need to create an index
    I might be wrong on these in the context of datawarehouse
    cheers
    css_jay99

    If the number of Y vs. N is about 50/50, you only have to parse through 50% of the
    overall amount of columns.Fnord. If the ratio of Y to N was 1:20 then the index would be useful for searching for values of Y only. Or if the column is nullable and the bulk of the entries were null then it might be useful. But a 50/50 split? FTS is much more effective.
    So, it does depend on the data distribution, but in most cases such an index would never be used (or should never be used).
    Cheers, APC

  • Updating a column of a table

    Hi all
    I have a problem in updating one column in the target table(say table A) from a column in the source table (say table B).
    All the columns in table A has been populated. Just one column should be populated. My source is a SQL object that contains a complex sql query that returns three columns date_from, date_to and ID.
    In the table A, I have the same date_from and date_to and need to get the ID from the SQL object and set it to the corresponding ID in the table A.
    I tried many ways to update the table, but it I couldn't ;(
    I should read from a table and update it based on a look-up function mapped to the column I need to update.
    I want to map the ID colmn from table A to a look-up ext function that can get the column from the table B. However I don't know how to use the Data Services objects to just update 1 column. If you could give me a consequence of the DS objects to be update a table based on a look-up , I would be so grateful.
    Thanks.

    Your dataflow should look like this :
    1) Source = the SQL transform with date_from, date_to and ID as columns.
    2) Next add a query (copy all columns from schema in to schema out) and set date_from and date_to as keys. Also make sure column names exactly match the column names in your target table, if not rename them in this query.
    3) Add a Map_Operation transform that maps all "normal" operation codes to "update"
    4) Finally add your target table and in the properties/options set "use input keys" to yes (or checked).
    This will generate UPDATE statements for all columns used in the query with a WHERE clause based on the keys defined in the query (date_from, date_to).
    Ben.

  • Navigation to update a column in the table jtf.jtf_rs_resource_extns

    Hi All,
    I'm using oracle 11.5.10
    Can somebody tell me the frontend form which is related to the table jtf.jtf_rs_resource_extns
    I need to update a column in the above table from the frontend.
    Please explain me the navigation for doing the same
    Regards,
    Mohan

    I can't tell you the answer to your query but what I can tell you is that you have logged the thread in a wrong forum. As far as my knowledge goes, this table does not delong to HRMS.
    Just check the product in which you are working and then log the thread in the right forum.
    Thanks,

  • Getting errors when updating a column on a table having a primary key

    Hi,
    I have an application on Oracle APEX that raises the following error after an attempt (through the application) to update a column with no specific constraint on it:
    ORA-06550: line 1, column 17: PL/SQL: ORA-00936: missing expression ORA-06550: line 1, column 9: PL/SQL: SQL Statement ignoredUnable to fetch row.
    The involved table has a primary key conatraint and the corresponding column can be populated by a sequence (but there is no trigger to manipulate the sequence).
    The sequence is mentioned in the involved page definition for populating the primary key.
    If I disable the primry key and set to null the corresponding value for the primary of the record to be updated, then it is possible to update that record (thus the above column) through the application.
    Did someone encountered this situation before?
    If yes, what was then your workaround/solution?
    Kind Regards.

    Dear user8058501 ,
    Firstly) Did you check
    Auto Row Fetch (After upgrade to 4.0.1)
    Automated Row Fetch on Table with Synonym causes ORA-00936: missing expr.
    Secondly) If the problem is not resolved, Would you provide a sample on apex.oracle.com with workspace/developer account to be able to help you
    Please, if this solves your question, mark it as Correct. Otherwise as helpful.
    Best Regards
    Mahmoud

  • Updating a column from other table

    Hi all,
    I have a simple update query problem. I have four tables
    activist(activist_id,first_name,last_name,c_state),
    membership(activist_id,g_n_id),
    group_network(g_n_id,g_n_type_id),
    school_grop_det(g_n_id,state). For some records in activist table the c_state column is null, i want to update that column with state column of school_group_det table.
    Here is the query for the states which are null
    select distinct a.activist_id,a.first_name,a.last_name,
    a.c_state,sd.state from activist a,membership m,
    group_network g,school_group_det sd where
    a.activist_id=m.activist_id and g.g_n_id=m.g_n_id and
    g.g_n_id=sd.g_N_id and a.c_state is null and g.g_n_type_id='1001'
    order by a.activist_id
    I got the activist_id,first_name,last_name,c_state from activist and state from school_group_det. now i as i told you want to update the c_state with state column of school_group_Det table.
    Pleae any one help me
    Thanks
    Srinivas

    For ur query the reply whay u have got is correct one. For further quries similar to one u had u can download a tool named TOAD (Tools for Oracle Application Developer) from quest site (www.quest.com). Install it a proceed.
    Do let me know how and what u feek about TOAD.

  • Updating the column of a table

    Hello Gurur,
    I have two tables they are
    Test & emp_op
    Desc  empop
    - empid
    -id_num
    -node_num
    _Desc Test
    -id_num
    -node_num
    -name
    -addr
    -empid
    Now i m trying to update the column empid in test table, copying as it is from empid column of emp_op table . Below is the query i have wriiten to update
    update test e
    set e.empid = (select t.empid from emp_op t
    where t.id_num = e.id_num and
    t.node_num = e.node_num );
    But it gives me an error ORA-01427: single-row subquery returns more than one row. any idea why ??
    if not, is there is any way i can accomplish this task in a different way ??
    Thank you so much. I appriciate it!!

    Hi,
    That means some row (maybe more than one) in test matches two or more rows in emp_op.
    What do you want to do in that case? Post some sample data (from both tables, relevant columns only) and the results you want to see in test after the UPDATE.
    If you know that all the matching rows in emp_op will have the same empid, you can just change SELECT in the sub-query to SELECT DISTINCT .

  • Need to update corresponding columns in details table

    Hi,
    I have a header and lines table.
    In header table we have a comments field, requirement is whenever someone is updating comments in header it should update lines comment column as well.
    I tried using fire action on Comments field like as soon as user is entering something in Comments field I am capturing that event and firing and update statement for lines table
    but the problem here is, if user enters the comments and doesn't save it at header level it stills fire an update statement in lines and updating the comment field of lines table.
    I need to update header comments in lines level at Apply button.
    Please someone suggest something.
    Best Regards
    Ragni Gupta

    Hi Gurus/Expert,
    Please suggest something, its really urgent!!
    Thanks
    Ragni Gupta

  • Need  trigger when update one column then record sysdate on another collumn

    Hi
    Gurus
    I have a table called RTV_PLAN
    And it have two columns,
    FORCE_KEYIN_FLAG VARCHAR(2)
    FORCE_KEYIN_DATE DATE
    I want when FORCE_KEYIN_FLAG get a value
    Then FORCE_KEYIN_DATE record the sysdate
    Please kindly give some help
    Many thanks
    saven

    Hi
    Justin
    thank you!
    It works for me very well.
    I use this code before
    <div class="jive-message-body"><div class="jive-quote"><span class="jive-quote-header"> if updating then
    </div>
    with your help my code is below now,
    <div class="jive-message-body"><div class="jive-quote"><span class="jive-quote-header">
    create or replace TRIGGER "RTV_PLAN_TRIG"
    before insert or update on RTV_PLAN
    for each row
    declare
    SCRAP_flag rtv_plan.SCRAP_flag%TYPE;
    force_keyin_flag rtv_plan.force_keyin_flag%TYPE;
    begin
    if inserting then
    if :NEW.RTV_PLAN_KEY is null then
    :NEW.RTV_PLAN_KEY := SAVEN.RTV_PLAN_api.RTV_gen_pk;
    end if;
    :NEW.dc_receive_date := sysdate;
    end if;
    if ( :new.scrap_flag IS NOT NULL ) then
    :NEW.SCRAP_date := sysdate;
    :NEW.SCRAP_USER := nvl(v('APP_USER'),USER);
    end if;
    if ( :new.force_keyin_flag IS NOT NULL )
    then
    :NEW.FORCE_KEYIN_date := sysdate;
    :NEW.FORCE_KEYIN_USER := nvl(v('APP_USER'),USER);
    end if;
    end;
    </div>
    can you checking this whether they have other error ?
    thank you
    saven

  • Need to update multiple columns using another table

    I have 2 tables. and i need to update rows of 1 table using another table
    Table1
    Serial_no.     payment_date     Payment_amt
    101     22/11/2010     150
    101     18/03/2011      355
    102     15/04/2011      488
    103     20/05/2011      178
    102     14/06/2011      269
    101     28/06/2011      505
    Table2
    Serial_no     Charge_amt      Last_paymt_dt     Last_paymt_amt
    101     255
    102     648
    103     475
    I want to update Last_paymt_dt and Last_paymt_amt of table2 using Table1, I have written following update statement but it gives error that single row subquery return multiple row.
    Update Table2
    set (Last_paymt_dt,Last_paymt_amt) = (select max(payment_date, payment_amt) from table1
    where table1.Serial_no = table2.Serial_no group by payment_amt)
    kindly suggest how should i update.

    SQL> select * from table1
      2  /
    SERIAL_NO PAYMENT_DA PAYMENT_AMT
           101 22/11/2010         150
           101 18/03/2011         355
           102 15/04/2011         488
           103 20/05/2011         178
           102 14/06/2011         269
           101 28/06/2011         505
    6 rows selected.
    SQL> select * from table2
      2  /
    SERIAL_NO CHARGE_AMT LAST_PAYMT LAST_PAYMT_AMT
           101        255
           102        648
           103        475
    SQL> update  table2
      2     set  (last_paymt_dt,last_paymt_amt) = (
      3                                            select  max(payment_date),
      4                                                    max(payment_amt) keep(dense_rank last order by payment_date)
      5                                              from  table1
      6                                              where table1.serial_no = table2.serial_no
      7                                           )
      8  /
    3 rows updated.
    SQL> select * from table2
      2  /
    SERIAL_NO CHARGE_AMT LAST_PAYMT LAST_PAYMT_AMT
           101        255 28/06/2011            505
           102        648 14/06/2011            269
           103        475 20/05/2011            178
    SQL> SY.

  • Need to update one column of all table in that particular schema

    hi all,
    i am using db10g.
    my task is to update one particular column's value of all the table in that schema.
    to acheive the above
    do i have to write update statement for each table or is there any way to do it in bulk?
    i hope my question make sense.
    Thanks..

    Hi karthick,
    I want to know something which is not related to this thread .
    How to include formatted post while replying to thread .
    I know using will be used  format  code before posting  . But how can we  do  it for posted message .
    I hope you understand my question .
    Thanks in advance .                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Maybe you are looking for

  • Vendor postings with purchase order

    hi, I am learning SAP Fi/co.I have one doubt .what are steps we have to maintain to post vendor transactions with purchase order.if u have any material about mm plz send to me.

  • UTF-8 Problems in Flex Builder 3

    I created locale properties files that contained characters requiring UTF-8 encoding using Flex Builder 3 for Mac OS. Some of the characters displayed incorrectly when viewed through the web browser. Open the properties files with an editor (BBEdit)

  • Help sending a Magic Packet through to WOL

    I'm trying to set up my Airport Extreme to send a magic packet through. It successfully wakes up my computer if I do it immediately after it goes to sleep, but if I try after its been asleep a few hours it doesn't work. I would think it may be a comp

  • HT4890 my PC tells me that "ApplePhotoStream.exe has stopped working".  How can I resolve this issue?

    "ApplePhotoStream.exe has stopped working"  How do I resolve this issue?

  • Transfer Business Partner from R/3 to CRM

    Hi All, Can any one pls help me how to transfer business partner from R/3 to CRM system? If it can be done through ALE pls explain me the detailed step how to do it like wht message type should be used etc. If there are some other way pls let me know