MySql Query Replace NULL with Empty String in Select

后端 未结 11 1170
天命终不由人
天命终不由人 2021-02-01 11:36

How do you replace a NULL value in the select with an empty string? It doesnt look very professional to output \"NULL\" values.

This is very unusual and based on my synt

11条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 12:32

    SELECT COALESCE(prereq, '') FROM test
    

    Coalesce will return the first non-null argument passed to it from left to right. If all arguemnts are null, it'll return null, but we're forcing an empty string there, so no null values will be returned.

    Also note that the COALESCE operator is supported in standard SQL. This is not the case of IFNULL. So it is a good practice to get use the former. Additionally, bear in mind that COALESCE supports more than 2 parameters and it will iterate over them until a non-null coincidence is found.

提交回复
热议问题