SQL set values of one column equal to values of another column in the same table

前端 未结 5 2001
[愿得一人]
[愿得一人] 2021-02-02 05:11

I have a table with two DATETIME columns.

One of them is never NULL, but one of them is sometimes NULL.

I need to write a query which will set all the NULL rows

5条回答
  •  梦如初夏
    2021-02-02 05:16

    I would do it this way:

    UPDATE YourTable SET B = COALESCE(B, A);
    

    COALESCE is a function that returns its first non-null argument.

    In this example, if B on a given row is not null, the update is a no-op.

    If B is null, the COALESCE skips it and uses A instead.

提交回复
热议问题