How about this query?
;with CTE as(
select case when A.Name is not null then A.Name else B.Name end as [Name],
case when A.Code is not null then A.Code else B.Code end as [Code],
case when A.PerID is not null then A.PerID else B.PerID end as [PerID],
CASE WHEN A.City = B.City THEN '' ELSE isnull(A.City, B.City) END AS City,
CASE WHEN A.Post = B.Post THEN '' ELSE isnull(A.Post, B.Post) END AS Post,
CASE WHEN A.Address = B.Address THEN '' ELSE isnull(A.Address, B.Address) END AS Address,
CASE WHEN A.Date1 = B.Date1 THEN '' ELSE
isnull(convert(VARCHAR(20),A.Date1,23), convert(VARCHAR(20),B.Date1,23)) END AS Date1,
case when A.Name is null then 'Missing in Table-A'
when B.Name is null then 'Missing in Table-B'
when A.Name = B.Name then '' end as Comments
from TableA as A FULL outer join TableB as B on A.Name = B.Name)
Select * from CTE where city is not null
or post is not null or address is not null
or date1 is not null or comments is not null;
Output:
Best regards,
Percy Tang
If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.