Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Update column based on another column SQL

  • 21-12-2015 4:11pm
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    I have an Oracle query to update 2 column of table. I only want to update the second column if the first a third column is not null.
    update tableA
    set 
       columnA = 100,
       Case When columnB is not null Then columnC = 300 End
    where 
       columnD=60;
    
    

    My Oracle is complaining that 'Found When, expecting = -or-'

    Anyone know why?


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Oracle is not my strong point, but looking at the oracle docs, it should be something more like
    update tableA
    set 
       columnA = 100,
       columnC = (SELECT Case WHEN columnB is not NULL THEN '300' ELSE ColumnC End)
    where 
       columnD=60;
    


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    You are missing your ELSE in the case.
    You need to tell Oracle what to display if your WHEN condition is false.
    update tableA
    set 
       columnA = 100,
       CASE
           WHEN columnB is not null THEN columnC = 300 
           ELSE 12345
       END
    where 
       columnD=60;
    


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    I found the answer:
    
    update tableA
    set 
       columnA = 100,
       columnC = Case When columnB is not null Then 300 Else null End
    where 
       columnD=60;
    


  • Registered Users, Registered Users 2 Posts: 10,906 ✭✭✭✭28064212


    That version is setting columnC to null when columnB is not null. What if columnC has a value already, and columnB is not null?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    28064212 wrote: »
    That version is setting columnC to null when columnB is not null. What if columnC has a value already, and columnB is not null?

    Isn't it setting columnC to NULL, unless columnB has a value?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 10,906 ✭✭✭✭28064212


    Isn't it setting columnC to NULL, unless columnB has a value?
    Your code:
    • If colB has a value, set colC to 300
    • If colB is NULL, set colC to NULL
    If colC is 400 originally, and colB is NULL, should the new value of colC be 400 or NULL?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    28064212 wrote: »
    Your code:
    • If colB has a value, set colC to 300
    • If colB is NULL, set colC to NULL
    If colC is 400 originally, and colB is NULL, should the new value of colC be 400 or NULL?

    I see your point. In my script, in your scenario, columnC's value of 400 will be overridden by NULL.

    In my case, columnC will never have a value if columnB is null. ColumnB is a timestamp and columnC is a timezone value.

    e.g.

    ColumnB | ColumnC
    6.00pm | GMT

    Do you think the following is better?
    update tableA
    set 
       columnA = 100,
       columnC = Case When columnB is not null Then 300 Else columnC End
    where 
       columnD=60;
    


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    I believe the NVL2 function does what you need, so you could do columnB = NVL2(columnB, 300, columnC) for instance.


  • Closed Accounts Posts: 5,361 ✭✭✭Boskowski


    I found the answer:
    
    update tableA
    set 
       columnA = 100,
       columnC = Case When columnB is not null Then 300 Else null End
    where 
       columnD=60;
    

    Correct, the case/when statement comes after the assignment. In your first example you would assign columnC a value only some of the time. But for every record that qualifies for the update you need to have the same set of updated columns apparently. If you want to retain the original you'd need another when where it goes columnC = columnC.


Advertisement