Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Comparing values in two tables

Author  Topic 

mageshks
Yak Posting Veteran

59 Posts

Posted - 2008-12-03 : 21:49:38
Hi,
I need a query to compare values in two different tables.when i use EXCEPT operator , it gives the entire row .But i want only the changed column value.

create table test1
(
col1 varchar(10)
,col2 varchar(10)
,col3 varchar(10)
,col4 varchar(10)
)

create table test2
(
col1 varchar(10)
,col2 varchar(10)
,col3 varchar(10)
,col4 varchar(10)
)


INSERT INTO TEST1 VALUES('1','TEST2','TEST3','TEST4')

INSERT INTO TEST2 VALUES('1','CHANGE','TEST3','TEST4')

I need the output as follows

col1 col2 col3 col4
------------------------
change


Thanks in advance...
Magesh

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-03 : 23:50:29
[code]select case when t1.col1<>t2.col1 then col1 else null end as col1,
case when t1.col2<>t2.col2 then col2 else null end as col2,
case when t1.col3<>t2.col3 then col3 else null end as col3,
case when t1.col4<>t2.col4 then col4 else null end as col4
from test1 t1
inner join test2 t2
on t1.pk=t2.pk[/code]

pk is primary key
Go to Top of Page
   

- Advertisement -