Solved

Determine differeing column

Posted on 2014-09-22
5
130 Views
Last Modified: 2014-10-08
Hi,
I am using except to return the differing rows in two select statements.
How would I be able to determine which column(s) is the different?

SELECT      Col1,
            Col2,
            Col3,
            Col4,
            Col5
FROM      Table1
EXCEPT
SELECT      Col1,
            Col2,
            Col3,
            Col4,
            Col5
FROM      Table2

Thanks
0
Comment
Question by:Morpheus7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336886
I'm afraid your question does not give all the information I need to answer it fully. Whether a column is numeric, or a date, or a varchar is important.

I am going to assume that Col1 is your primary key, and it is the same between Table1 and Table2.

Here's my answer:

select T.col1, T.col2, T.col3, T.col4, T.col5,
iif(T.col2<>U.col2,1,0) as Col2Different, 
iif(T.col3<>U.col3,1,0) as Col3Different, 
iif(T.col4<>U.col4,1,0) as Col4Different, 
iif(T.col5<>U.col5,1,0) as Col5Different
from table1 T
left join table2 U
on T.col1 = U.col1

Open in new window

0
 

Author Comment

by:Morpheus7
ID: 40336957
Many thanks for the reply. Col1 is the primary key and the others are varchar with the exception being col5 which is int.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40338511
Are any of the columns nullable?
0
 

Author Comment

by:Morpheus7
ID: 40338549
Hi,

With the exception of Col1, they could be nullable
Thanks
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40338564
Here's my updated answer. It shows 0 if there is no difference, 1 if there is a difference, and 2 if the row is new:

select T.col1, T.col2, T.col3, T.col4, T.col5,
iif(coalesce(T.col2,'')<>coalesce(U.col2,''),iif(U.col2 is null,2,1),0) as Col2Different, 
iif(coalesce(T.col3,'')<>coalesce(U.col3,''),iif(U.col3 is null,2,1),0) as Col3Different, 
iif(coalesce(T.col4,'')<>coalesce(U.col4,''),iif(U.col4 is null,2,1),0) as Col4Different, 
iif(coalesce(T.col5,-2147483648)<>coalesce(U.col5,-2147483648),iif(U.col5 is null,2,1),0) as Col5Different
from table1 T
left join table2 U
on T.col1 = U.col1

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question