sql - 在 SQL Server 查询中将 NULL 替换为 0

标签 sql sql-server

我开发了一个查询,在前三列的结果中我得到NULL。如何将其替换为 0

Select c.rundate, 
  sum(case when c.runstatus = 'Succeeded' then 1 end) as Succeeded, 
  sum(case when c.runstatus = 'Failed' then 1 end) as Failed, 
  sum(case when c.runstatus = 'Cancelled' then 1 end) as Cancelled, 
  count(*) as Totalrun from
  (    Select a.name,case when b.run_status=0 Then 'Failed' when b.run_status=1 Then 'Succeeded'
  when b.run_status=2 Then 'Retry' Else 'Cancelled' End as Runstatus,
  ---cast(run_date as datetime)
              cast(substring(convert(varchar(8),run_date),1,4)+'/'+substring(convert(varchar(8),run_date),5,2)+'/'          +substring(convert(varchar(8),run_date),7,2) as Datetime) as RunDate
  from msdb.dbo.sysjobs as a(nolock) inner join msdb.dbo.sysjobhistory as b(nolock) 
  on a.job_id=b.job_id
  where a.name='AI'
  and b.step_id=0) as c
  group by 
  c.rundate

最佳答案

当您想用其他内容替换可能为空的列时,请使用 IsNull .

SELECT ISNULL(myColumn, 0 ) FROM myTable

如果 myColumn 一开始就为空,这将在 myColumn 中放入 0。

关于sql - 在 SQL Server 查询中将 NULL 替换为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840522/

相关文章:

sql-server - 恢复备份时,如何断开所有事件连接?

php - 如何防止 PHP 中的 SQL 注入(inject)?

sql - sql游标的替代品

sql - 看似简单的 SQL 查询结果非常复杂 (Oracle)

sql - 提高添加具有单个值的列的性能

sql-server - 如何获取数据类型为 varchar 且第一个字符为 "£"的行的总和?

php - 在给定的时间跨度内发生了多少天

sql - 连接两个具有逗号分隔列的表的最佳方法是什么

android - 带有服务器和数据库的移动应用程序中的 Paypal 支付流程

sql - 如何使用 JSON_VALUE 对 JSON 数据设置多个过滤器?