代码之家  ›  专栏  ›  技术社区  ›  iLemming

空字段的ISNULL等效值

  •  2
  • iLemming  · 技术社区  · 14 年前

    是否有类似ISNULL()或COALESCE()的东西,但不是检查null值而是检查空值。

    例如:

      SELECT cu.last_name, cu.first_name, cu.email, hu.email FROM 
     (SELECT DISTINCT c.first_name, c.last_name, c.email, c.household_id, h.head_of_household_id
      FROM rd_customers c
      JOIN rd_households h ON c.household_id = h.household_id
      JOIN ad_registrations r ON r.customer_id = c.customer_id
      JOIN ad_meeting_times a ON  r.session_id = a.session_id and a.meeting_time_id = 203731) cu
      LEFT JOIN rd_customers hu ON hu.customer_id = cu.head_of_household_id
    

    ISNULL和COALESCE在这里不起作用我不知道为什么

    3 回复  |  直到 14 年前
        1
  •  8
  •   Andomar    14 年前

    您可以始终使用用例:

    select  case when cu.email is null or cu.email = '' then hu.email 
                 else cu.email end as ColumnName
    from    YourTable
    
        2
  •  3
  •   KM.    14 年前

    看看 NULLIF (Transact-SQL)

    NULLIF返回第一个表达式if 表达式相等,如果为空

    试试这个:

    COALESCE(NULLIF(cu.email,''),NULLIF(hu.email,''))
    

    如果特写邮件以及hu.E邮箱是空字符串或NULL的任意组合

        3
  •  1
  •   Nathan Wheeler    14 年前

    这将捕获null或空特写邮件使用hu.E邮箱取而代之的是:

    SELECT cu.last_name, 
           cu.first_name, 
           CASE WHEN ISNULL(cu.email, '') = '' THEN
                hu.email
           ELSE
                cu.email
           END AS email
    FROM 
    (
      SELECT DISTINCT c.first_name, 
                      c.last_name, 
                      c.email, 
                      c.household_id, 
                      h.head_of_household_id
      FROM rd_customers c
      JOIN rd_households h 
        ON c.household_id = h.household_id
      JOIN ad_registrations r 
        ON r.customer_id = c.customer_id
      JOIN ad_meeting_times a 
        ON  r.session_id = a.session_id 
          AND a.meeting_time_id = 203731
    ) cu
    LEFT JOIN rd_customers hu 
      ON hu.customer_id = cu.head_of_household_id