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

当前回答

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

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
UPDATE TableName SET ColumnName= ISNULL(ColumnName, 0 ) WHERE Id = 10

一个简单的方法是

UPDATE tbl_name SET fild_name = value WHERE fild_name IS NULL

如果你正在使用Presto, AWS Athena等,没有ISNULL()函数。相反,使用:

SELECT COALESCE(myColumn, 0 ) FROM myTable

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

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的完整文档。