如何在SQL中用n / a替换null?

时间:2019-02-08 02:44:24

标签: sql sql-server

我有一个代码,返回一个integer数据类型,否则返回null。现在,对于null值,我想将其替换为'N/A'。我尝试使用替换,但它没有改变。

示例输出:

2014-FW RTW1005F4   NULL

代码:

select a.Season_id,a.Style_id,
  ( case when ((( a.Content_class = 'ART' and a.Mat_ref_id in (select  Mat_ref_id from iplexmat_material_parm where artwrk_type='EMB'))))
       then count(a.Mat_ref_id)
    else null
  end ) as Embro
from iPLEXSTY_AD_BOM_DTL a
inner join iPLEXMAT_MATERIAL_PARM b on b.Mat_ref_id = a.Mat_ref_id
where b.artwrk_type is not null and a.Style_id = 'RTW1005F4' 
group by a.Season_id,a.Style_id,a.Content_class,a.Mat_ref_id

3 个答案:

答案 0 :(得分:3)

如果要返回'N/A',则必须将值转换为字符串:

(case when a.Content_class = 'ART' and
           a.Mat_ref_id in (select  Mat_ref_id from iplexmat_material_parm where artwrk_type = 'EMB')
      then convert(varchar(255), count(a.Mat_ref_id))
      else 'N/A'
 end) as Embro

答案 1 :(得分:0)

使用coalesce

Select coalesce(column,'n/a')

当列的值为null时,它将返回n / a,但是该列必须是字符串类型,因为您的列是int,因此应将其转换为字符串类型

答案 2 :(得分:0)

您可以尝试一下。

select a.Season_id,a.Style_id,
  ISNULL(( case when ((( a.Content_class = 'ART' and a.Mat_ref_id in (select  Mat_ref_id from iplexmat_material_parm where artwrk_type='EMB'))))
       then count(a.Mat_ref_id)
    else null
  end ),'N/A') as Embro
from iPLEXSTY_AD_BOM_DTL a
inner join iPLEXMAT_MATERIAL_PARM b on b.Mat_ref_id = a.Mat_ref_id
where b.artwrk_type is not null and a.Style_id = 'RTW1005F4' 
group by a.Season_id,a.Style_id,a.Content_class,a.Mat_ref_id