?
Solved

Determine differeing column

Posted on 2014-09-22
5
Medium Priority
?
133 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.
Suggested Courses

764 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