代码之家  ›  专栏  ›  技术社区  ›  Kgn-web

sql中Select查询中的大小写

  •  -1
  • Kgn-web  · 技术社区  · 7 年前

    下面是我的SQL语句和case语句。

    select 
        a.AssociationClubNumber,
        case a.AssociationClubNumber
            when NULL then 'NA'
            when '' then 'NA'
            else a.AssociationClubNumber
        end as 'AssociationClubNumber'
    from 
        [dbo].[Customer] a
    

    a.AssociationClubNumber 是 NULL "" ,然后 CASE 应返回字符串“NA”。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Gordon Linoff    7 年前

    case --通过明确的比较:

    select c.AssociationClubNumber,
           (case when c.AssociationClubNumber is null or 
                      c.AssociationClubNumber = ''
                 then 'NA'
                 else c.AssociationClubNumber
            end)  as new_AssociationClubNumber
    from [dbo].[Customer] c;
    

    您正在使用简单的 案例 . 即使是简单的形式,, NULL = 无效的 .

    另请注意:

    • 我更改了列别名,因此返回的两列是不同的。
    • 我删除了列别名周围的单引号。仅对字符串和日期常量使用单引号。
        2
  •  0
  •   Zaynul Abadin Tuhin    7 年前

    is null

     select a.AssociationClubNumber,
        Case a.AssociationClubNumber
        When is NULL THEN  'NA'  
        WHEN  '' then  'NA'
        ELSE a.AssociationClubNumber
        END  as AssociationClubNumber_val
        from [dbo].[Customer] a
    
        3
  •  0
  •   Zoe - Save the data dump 张群峰    7 年前

    CASE [value] WHEN NULL THEN END CASE WHEN [value] IS NULL THEN 1 END .

    ISNULL(NULLIF(AssociationClubNumber,'')'NA')