column level differences SQL

Rakesh Dhar 96 Reputation points
2021-10-26T08:54:13.627+00:00

Hi Friends ,
Need help in My requirements implementation .

I need to implement the column level difference, (not a set based operation) need to compare the 2 tables columns and show the result set .

T1
c1 c2 c3

T2
c1 c2 c3

need to compare .....T1.c1=t2.c1

please help how this can be implemted .please share any reference links

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,490 questions
SQL Server Migration Assistant
SQL Server Migration Assistant
A Microsoft tool designed to automate database migration to SQL Server from Access, DB2, MySQL, Oracle, and SAP ASE.
547 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,690 questions
{count} votes

4 answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,601 Reputation points
    2021-10-26T09:04:16.387+00:00

    Which columns do you want to compare the differences between? Please provide some sample data.

    Perhaps the NULLIF function is useful for you:

    SELECT *,NULLIF (T1.c2,T2.c2) c2diff,NULLIF (T1.c3,T2.c3) c3diff  
    FROM T1  
    JOIN T2 ON T1.c1=T2.c1  
     
    

    For details, please refer to:
    NULLIF (Transact-SQL)

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.


  2. Olaf Helper 46,031 Reputation points
    2021-10-26T09:09:27.403+00:00

    That's to less on information.

    Please post table design as DDL, some sample data as DML statement and the expected result.

    0 comments No comments

  3. Rakesh Dhar 96 Reputation points
    2021-10-26T09:48:06.813+00:00

    more description :-

    I do have Bank Branch DB with table as Payment_tbl which is being compared with the Main Branch DB .with same table name

    branch.payment_tbl is being compared with MainBranch.payment_tbl

    both the tables are having equal number of columns as well data is also equal as being filtered on the basis of branchid as well accountid .

    now ..,
    nullif( branch.payment_tbl.seq_nbr,'') <> nullif(mainbranch.payment_tbl.seq_nbr)

    this way ...columns are being compared right now . count of columns varied from 20/40 columns

    this is a set based operation . i dont want to go for cursor based operation .

    is there any other way in SQL to implement this comparison ... .?


  4. EchoLiu-MSFT 14,601 Reputation points
    2021-10-27T02:29:00.52+00:00

    Please try:

        CREATE TABLE #diff(c1 datetype,c2 datetype,c3 datetype,  
        diff2 datetype,diff3 datetype)  
           
          
        DECLARE @i INT  
          
        SET @i=1  
          
        WHILE @i<=(SELECT COUNT(c1) FROM T1)  
        BEGIN   
          IF  (SELECT NULLIF (T.c2,T2.c2)  
          FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY c1) rr FROM T1) T  
          JOIN T2 ON T.c1=T2.c1  
          WHERE rr=@i) IS NOT NULL  
          
          INSERT INTO #diff   
          SELECT T.c1,T.c2,T.c3,NULLIF (T.c2,T2.c2) c2diff,NULLIF(T.c3,T2.c3) c3diff  
          FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY c1) rr FROM T1) T  
          JOIN T2 ON T.c1=T2.c1  
          WHERE rr=@i  
          
          SET @i=@i+1  
          
        END  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.