218

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

13 回答 13

455

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

SELECT ISNULL(myColumn, 0 ) FROM myTable

如果 myColumn 首先为 null,这将在 myColumn 中放置一个 0。

于 2013-05-30T15:49:21.860 回答
91

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

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

比较 COALESCE() 和 ISNULL():

  1. ISNULL 函数和 COALESCE 表达式具有相似的目的,但可以表现不同。

  2. 因为 ISNULL 是一个函数,所以它只计算一次。如上所述,可以多次计算 COALESCE 表达式的输入值。

  3. 结果表达式的数据类型确定是不同的。ISNULL 使用第一个参数的数据类型,COALESCE 遵循 CASE 表达式规则,返回优先级最高的 value 的数据类型。

  4. 对于 ISNULL 和 COALESCE,结果表达式的 NULLability 是不同的。ISNULL 返回值始终被认为是不可为空的(假设返回值是不可为空的),而具有非空参数的 COALESCE 被认为是 NULL。因此,表达式 ISNULL(NULL, 1) 和 COALESCE(NULL, 1) 虽然等效,但具有不同的可空性值。如果您在计算列中使用这些表达式、创建键约束或使标量 UDF 的返回值具有确定性以便可以按以下示例中所示对其进行索引,这会有所不同。

-- 该语句失败,因为 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 函数的可空性评估为 AS NOT NULL。

CREATE TABLE #Demo 
( 
    col1 integer NULL, 
    col2 AS COALESCE(col1, 0), 
    col3 AS ISNULL(col1, 0) PRIMARY KEY 
);
  1. ISNULL 和 COALESCE 的验证也不同。例如,ISNULL 的 NULL 值转换为 int,而对于 COALESCE,您必须提供数据类型。

  2. ISNULL 仅采用 2 个参数,而 COALESCE 采用可变数量的参数。

    如果您需要了解更多信息,请参阅 msdn的完整文档

于 2014-11-14T06:42:32.460 回答
26

coalesce

coalesce(column_name,0)

虽然,在哪里 summing when condition then 1,您可以轻松更改sumcount- 例如:

count(case when c.runstatus = 'Succeeded' then 1 end) as Succeeded,

Count(null)返回 0,而sum(null)返回 null。)

于 2013-05-30T15:49:03.080 回答
13

当您说前三列时,您是指您的SUM列吗?如果是这样,请添加ELSE 0到您的CASE陈述中。的值SUM是。NULLNULL

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, 
于 2013-05-30T15:49:44.947 回答
11

一个简单的方法是

UPDATE tbl_name SET fild_name = value WHERE fild_name IS NULL
于 2016-07-02T09:26:24.937 回答
8

如果您使用 Presto、AWS Athena 等,则没有 ISNULL() 函数。相反,使用:

SELECT COALESCE(myColumn, 0 ) FROM myTable
于 2019-10-29T14:27:45.010 回答
7

将您的列包装在此代码中。

 ISNULL(Yourcolumn, 0)

也许检查你为什么得到空值

于 2013-05-30T15:50:14.407 回答
6

Use COALESCE,它返回第一个非空值,例如

SELECT COALESCE(sum(case when c.runstatus = 'Succeeded' then 1 end), 0) as Succeeded

如果返回为 ,则将 Succeeded 设置为 0 NULL

于 2013-05-30T15:49:50.790 回答
1

将 else 添加到您的 case 语句中,以便在未找到测试条件时它们默认为零。目前,如果未找到测试条件,则将 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
于 2013-05-30T15:50:29.710 回答
0

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

ISNULL(MyColumnName, 0) MyColumnName
于 2019-04-06T13:22:42.447 回答
0
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, 

这里的问题是,如果没有 else 语句,当运行状态不是列描述中规定的状态时,您一定会收到 Null。向 Null 添加任何内容都会导致 Null,这就是此查询的问题。

祝你好运!

于 2016-09-23T14:08:41.927 回答
0

对于常规 SQL,ISNULL(item)只能带一个参数,因此 90% 的这些解决方案都行不通。

我重新利用了@Krishna Chavali 的答案来做到这一点:

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

column_name如果不是null,这将返回 in 的值,如果0null

于 2021-12-29T05:56:04.497 回答
-4
UPDATE TableName SET ColumnName= ISNULL(ColumnName, 0 ) WHERE Id = 10
于 2020-11-27T11:20:39.587 回答