Question

I have an Access 2013 database with a table named [carInsurance] as follows:

enter image description here

Now I want to display all the data in this table along with an "empty" row full of NULL values. So I use a UNION query to accomplish that as follows:

select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance 
UNION 
select insId, carId, insFromDate from carInsurance 

However, the results show up like this

enter image description here

and when I use UNION ALL as follows:

select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance 
UNION ALL 
select insId, carId, insFromDate from carInsurance

I get all results correct except that I get an empty line for every result. How can I solve this problem?

Was it helpful?

Solution

I'm going to go on a hunch. I think it is because of different data types being chosen when you give it just NULL. Beats me why it would be different for UNION vs UNION ALL.

Try this:

Select 
    cast (NULL as int) as insId, 
    cast (NULL as int) as carId, 
    cast (NULL as Date) as insFromDate 
from carInsurance 
UNION  
Select insId, carId, insFromDate from carInsurance 

Extending the same line of thought, I would suggest is reversing the order of your SELECTs:

Select insId, carId, insFromDate from carInsurance 
UNION  
Select NULL as insId, NULL as carId, NULL as insFromDate from carInsurance 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top