在SQL中的情况

时间:2018-10-25 12:20:24

标签: sql tsql

当包含谁的两列包含data / null时,我有了表,我只想捕获不为null的行。

t1的示例

Application_channel | reason_for_joining
            call    | null
            null    | do not wish to provide
            null    | null

我希望输出为

Status
 call
 do not wish to provide

我写的查询有点像:-

select case 
when reason_for_joining is null and application_channel is not null then       application_channel
when application_channel is null and reason_for_joining is not null then 
reason_for_joining else application_channel end as status
from t1

问题是当两个列都具有我不想要的空值时,它也取空值。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:3)

他还想过滤那些空的,所以应该是:

select coalesce(Application_channel, reason_for_joining) as status
from t1
where coalesce(Application_channel, reason_for_joining) is not null

或者,您也可以像这样过滤:

WHERE Application_channel IS NOT NULL OR reason_for_joining IS NOT NULL

答案 1 :(得分:0)

您要coalesce()

select coalesce(Application_channel, reason_for_joining) as status
from t1

答案 2 :(得分:0)

两个参数ISNULL的情况也可以做同样的工作

select ISNULL('call',null),isnull(null,'do not')

因此您可以查询

select ISNULL(Application_channel, reason_for_joining) as status
from t1
where ISNULL(Application_channel, reason_for_joining) is not null