Skip to content
Advertisement

Get NULL values [closed]

How would I get a NULL value as well in this query:

SELECT x_month FROM end_months WHERE x_month = 201907

This shows the month because it exists but I also want to have another row if it doesnt exist (Not another column)

Advertisement

Answer

You can use the following technique:

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
)

The second query will add a row with null if the cte returns no results.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement