Solved

Determine differeing column

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now