Search code examples
sqlstockportfolio

SQL - replace a NULL value with a calculated value


My Query shows Stock tickers, the price target I have set for the stock, and the open price. If my price_target column has a NULL value, i want to replace it with a calculated value. 10% increase on the open.

enter image description here

So for example: row 3, ticker = ARCB. The price_target has a NULL value. The open is 43.7, so NULL should be replaced with 48.07.

I'm assuming I should use a CASE expression: (case WHEN price_target IS NULL THEN ((open * 0.1) + OPEN) END)

However, this creates a new column:

enter image description here

How do I just replace the NULL values in the price_target column?


Solution

  • Like this:

    SELECT ticker, 
        case WHEN price_target IS NULL THEN ((open * 0.1) + open) else price_target 
        END as price_target, 
        open 
    FROM rating_open