Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Compare 2 datasets and output delta in one TSQL statement

By Theo Ekelmans,

It's a common problem we all share from time to time, and i thought my solution is worth a post :)

You have 2 datasets and you wonder.....  has anything changed, and if so, did it change in A or B?

In my example i show a way to compare 2 sets of employee data and output the changes in one TSQL statement

Table A

# EmpNr Name HireDT FireDT
1 1 janssen 2000-01-01 00:00:00.000 {NULL}
2 2 de bruin 2001-05-01 00:00:00.000 2005-12-31 00:00:00.000
3 3 pieterssen 2004-08-01 00:00:00.000 {NULL}
4 4 de vries 2003-09-01 00:00:00.000 2011-01-01 00:00:00.000
5 5 Klaassen 2009-09-01 00:00:00.000 2011-01-01 00:00:00.000
6 6 van Speyk 2009-09-01 00:00:00.000 {NULL}
7 7 Klaassen 2009-09-01 00:00:00.000 2011-01-01 00:00:00.000

Table B

# EmpNr Name HireDT FireDT
1 2 de bruin 2001-05-01 00:00:00.000 2005-12-31 00:00:00.000
2 3 pietersse 2004-08-01 00:00:00.000 {NULL}
3 4 de vries 2003-09-01 00:00:00.000 2011-01-01 00:00:00.000
4 5 Klaassen 2009-09-01 00:00:00.000 2011-01-01 00:00:00.000
5 6 van Speijk 2009-09-01 00:00:00.000 {NULL}
6 7 Klaassen 2009-09-01 00:00:00.000 2011-01-01 00:00:00.000
7 8 de groot 2003-09-01 00:00:00.000 2011-01-01 00:00:00.000

The differences

# EmpNr_a EmpNr_b Name_a Name_b HireDT_a HireDT_b FireDT_a FireDT_b
1 1 {NULL} janssen {NULL} 2000-01-01 00:00:00.000 {NULL} {NULL} {NULL}
2 3 3 pieterssen pietersse 2004-08-01 00:00:00.000 2004-08-01 00:00:00.000 {NULL} {NULL}
3 6 6 van Speyk van Speijk 2009-09-01 00:00:00.000 2009-09-01 00:00:00.000 {NULL} {NULL}
4 {NULL} 8 {NULL} de groot {NULL} 2003-09-01 00:00:00.000 {NULL} 2011-01-01 00:00:00.000

As you can see, the output shows additions, removals and changes.

Happy Coding!


Theo Ekelmans

Sr MS-SQL DBA @ Ordina.nl

Total article views: 2084 | Views in the last 30 days: 9
 
Related Articles
FORUM

Do not display "1900-01-01 00:00:00.000"

How to void displaying ''1900-01-01 00:00:00.000" if datetime data is null? where is that displayi...

FORUM

want only date from date-time format

want only 1980-12-17 instead of 1980-12-17 00:00:00.000

FORUM

datetime => how to have complete date with 00:00:00.000

Hi, i work with sql server 2005 and i 'm a beginner I have a table with a column datetime. i...

FORUM

2004-05-13 00:00:00.000 What is the datatype for this string?

Does anyone recognise the data type for the following string?   2004-05-13 00:00:00.000  If there ...

FORUM

weired performance killing

Here is the part of the query that it taking too long (temp tables involved run in few seconds which...

Tags
compare data    
compare dataset    
delta    
script    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones