MySql Query Replace NULL with Empty String in Select

后端 未结 11 1140
天命终不由人
天命终不由人 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.

    0 讨论(0)
  • 2021-02-01 12:32

    The original form is nearly perfect, you just have to omit prereq after CASE:

    SELECT
      CASE
        WHEN prereq IS NULL THEN ' '
        ELSE prereq
      END AS prereq
    FROM test;
    
    0 讨论(0)
  • 2021-02-01 12:35
    select IFNULL(`prereq`,'') as ColumnName FROM test
    

    this query is selecting "prereq" values and if any one of the values are null it show an empty string as you like So, it shows all values but the NULL ones are showns in blank

    0 讨论(0)
  • 2021-02-01 12:39

    Some of these built in functions should work:

    Coalesce
    Is Null
    IfNull
    
    0 讨论(0)
  • 2021-02-01 12:43

    Try COALESCE. It returns the first non-NULL value.

    SELECT COALESCE(`prereq`, ' ') FROM `test`
    
    0 讨论(0)
提交回复
热议问题