获取NULL值

时间:2019-08-23 17:07:10

标签: sql sql-server

在此查询中,如何获得NULL值:

SELECT x_month FROM end_months WHERE x_month = 201907

这是显示月份,因为它存在,但是如果不存在,我还想再有一行(不是另一列)

3 个答案:

答案 0 :(得分:1)

使用子句OR

SELECT x_month
FROM end_months
WHERE
    x_month = 201907 
    OR x_month IS NULL

答案 1 :(得分:0)

您可以使用以下技术:

With cte as (
    SELECT x_month 
    FROM end_months 
    WHERE x_month = 201907
)

Select x_month
From cte
Union all
Select null
Where not exist (
    select 1 from cte
)

如果cte不返回结果,第二个查询将在行中添加null

答案 2 :(得分:0)

使用UNION ALL 并编写另一个不存在月份的选择查询

相关问题