Select Query to find a Range and Assign a new value

  • Hi Team

    i have below table with 3 columns ID, OPEN_CLOSE, Status. and blank values in status column.
    i need a query like, when there is an open status columns should be updated with started and when it finds close status columns should be udpated with Stopped.
    it is a scenario of power ON OFF on a particular day, how many times power on and OFF.

    IDOPEN_cLOSEStatus
    1OpenStarted
    2ClosedStopped
    3OpenStarted
    4Open 
    5Open 
    6ClosedStopped
    7Closed 
    8Closed 
    9OpenStarted
    10Open 
    11Open 
    12ClosedStopped
    13Closed 

    Please help me in developing the script.

  • If I understand correctly I think this will work:


    update status_table
         SET Status = CASE OPEN_CLOSE
                                         WHEN 'Open' THEN 'Started'
                                         WHEN 'Closed' THEN 'Stopped'
                                         ELSE NULL /* or empty string if the column doesn't allow null */
                                  END
    WHERE
           /* You don't really define how to know which rows to update so I'm guessing here */
          Status IS NULL; /* or Status = ''; If you don't allow NULLs */

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply