我已经开发了一个查询,在前三列的结果中,我得到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

当前回答

对于常规SQL, ISNULL(item)只能接受一个参数,因此90%的解决方案都不起作用。

我重新利用@Krishna Chavali的回答来说明:

(CASE WHEN (NOT ISNULL(column_name)) THEN column_name ELSE 0 END) AS ColumnName

如果column_name不为空,则返回该值;如果column_name为空,则返回0。

其他回答

在case语句中添加else,这样如果没有找到测试条件,它们默认为0。此时,如果没有找到测试条件,则将NULL传递给SUM()函数。

Select c.rundate, 
  sum(case when c.runstatus = 'Succeeded' then 1 else 0 end) as Succeeded, 
  sum(case when c.runstatus = 'Failed' then 1 else 0 end) as Failed, 
  sum(case when c.runstatus = 'Cancelled' then 1 else 0 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

对于常规SQL, ISNULL(item)只能接受一个参数,因此90%的解决方案都不起作用。

我重新利用@Krishna Chavali的回答来说明:

(CASE WHEN (NOT ISNULL(column_name)) THEN column_name ELSE 0 END) AS ColumnName

如果column_name不为空,则返回该值;如果column_name为空,则返回0。

你可以使用这两种方法,但有区别:

SELECT ISNULL(col1, 0 ) FROM table1
SELECT COALESCE(col1, 0 ) FROM table1

对比COALESCE()和ISNULL():

The ISNULL function and the COALESCE expression have a similar purpose but can behave differently. Because ISNULL is a function, it is evaluated only once. As described above, the input values for the COALESCE expression can be evaluated multiple times. Data type determination of the resulting expression is different. ISNULL uses the data type of the first parameter, COALESCE follows the CASE expression rules and returns the data type of value with the highest precedence. The NULLability of the result expression is different for ISNULL and COALESCE. The ISNULL return value is always considered NOT NULLable (assuming the return value is a non-nullable one) whereas COALESCE with non-null parameters is considered to be NULL. So the expressions ISNULL(NULL, 1) and COALESCE(NULL, 1) although equivalent have different nullability values. This makes a difference if you are using these expressions in computed columns, creating key constraints or making the return value of a scalar UDF deterministic so that it can be indexed as shown in the following example.

—此语句失败,因为PRIMARY KEY不能接受NULL值 ——以及col2的COALESCE表达式的可空性 ——求值为NULL。

CREATE TABLE #Demo 
( 
    col1 integer NULL, 
    col2 AS COALESCE(col1, 0) PRIMARY KEY, 
    col3 AS ISNULL(col1, 0) 
); 

对象的可空性使该语句成功 ——ISNULL函数的值为NOT NULL。

CREATE TABLE #Demo 
( 
    col1 integer NULL, 
    col2 AS COALESCE(col1, 0), 
    col3 AS ISNULL(col1, 0) PRIMARY KEY 
);

ISNULL和COALESCE的验证也是不同的。例如, ISNULL的NULL值转换为int,而COALESCE的NULL值转换为int, 您必须提供一个数据类型。 ISNULL只接受2个参数,而COALESCE接受一个变量 参数个数。 如果你需要了解更多,这里是来自msdn的完整文档。

通过遵循之前的答案,我在SQL server db中丢失了我的列名,但是遵循这种语法帮助我保留了ColumnName

ISNULL(MyColumnName, 0) MyColumnName

当你想用其他东西替换一个可能为空的列时,使用IsNull。

SELECT ISNULL(myColumn, 0 ) FROM myTable

这将在myColumn中放置一个0,如果它首先是空的。