I have a situation where in I have to combine two tables without losing any data. The two tables have different structures. Following is the structures of my tables
TABLE A
ID_NO INT,
Ship_Date DATE,
Status varchar(10),
total decimal(12,2)
TABLE B
ID_NO INT,
Status varchar(10),
total decimal(12,2)
I tried using UNION ALL by including a dummy column in TABLE B as follows
TABLE B
ID_NO INT,
'',
Status varchar(10),
total decimal(12,2)
but in the result set i get 1900-01-01 as Ship_Date instead of ''. How to eliminate this?
Use a NULL value instead of an empty string. If you don't mind the Ship_Date result as a string, then you can wrap the UNION in another select statement.
SELECT U._ID_NO,
CASE WHEN U.Ship_Date IS NULL
THEN ''
ELSE CONVERT(NVARCHAR(50), U.Ship_Date,101) END AS Ship_Date,
U.Status,
U.total
FROM
(
SELECT A.ID_NO, A.Ship_Date, A.Status, A.total
FROM TableA
UNION ALL
SELECT B.ID_NO, NULL AS Ship_Date, B.Status, B.total
FROM TableB
) AS U
Ship_Date
is a date
datatype, why not use NULL
as a dummy placeholder instead?
TABLE B
ID_NO INT,
NULL,
Status varchar(10),
total decimal(12,2)
You're getting 1900-01-01
because that column type is DATETIME. Use NULL
instead of ''
if you want it to be "empty".
Try:
select
ID_NO,
case
when Ship_Date is NULL then ''
else Ship_Date
end as Ship_Date,
Status,
total
from
(
select
ID_NO,
Ship_Date,
Status,
total
from
table_a
union all
select
ID_NO,
NULL as Ship_Date,
Status,
total
from
table_b
) combined
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.