Solved

Determine differeing column

Posted on 2014-09-22
5
127 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

839 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