Solved

Determine differeing column

Posted on 2014-09-22
5
126 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 I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

773 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