Set default value in query when value is null

前端 未结 6 1346
难免孤独
难免孤独 2021-02-04 08:16

I\'m running a really simple query, however for some of the results the value in one field is null. How can I set that value to \"a string\" if its value is null?

Someth

6条回答
  •  长情又很酷
    2021-02-04 08:56

    As noted above, the coalesce solution is preferred. As an added benefit, you can use the coalesce against a "derived" value vs. a selected value as in:

    SELECT
           {stuff},
           COALESCE( (select count(*) from tbl where {stuff} ), 0 )  AS countofstuff
    FROM
           tbl
    WHERE
           {something}
    

    Using "iif" or "case" you would need to repeat the inline whereas with coalesce you do not and it allows you to avoid a "null" result in that return...

提交回复
热议问题