设置将查询原结果为NULL的字段值置为0.0(数值型)或‘’(字符型)

来源:互联网 发布:宁夏慈善网络信息平台 编辑:程序博客网 时间:2024/06/18 15:14

--使用SQL Server自带之范例数据库pubs。其中原price字段类型为Money,notes字段类型为varchar(200)

--通用做法(好处在于不仅可以应用于转零,还可以转为任何想要的值):

select title, 'Price123' =
  case
    when price is null then 0.0
    else price
  end, type,
  'notes123' =
  case
    when notes is null then ''
    else notes
   end
from pubs.dbo.titles

--一般做法(仅限此处转零应用):

select
    title,
    IsNull(price, 0) as price123,
    type, 
    IsNull(notes, 'unnoted') as notes123
from pubs.dbo.titles

原创粉丝点击