Zman
Zman

Reputation: 5

SQL Server compare data

I am attempting to compare 2 columns in one SQL Table. Column1 has 012-0000430-001 and Column2 has 0120000430001 both nvarchar data types. I would like to run a compare to make sure both tables match.

select Column1,substring(Column2,1,3)
+ substring(Column2,5,7)
+substring(Column2,13,3)
from Table1

This query gives me the data but what could I do next to see which data matches and which does not. I would eventually like to create a trigger that find the mismatch and then correct it.

Thanks in advance!

Upvotes: 0

Views: 50

Answers (2)

Dgan
Dgan

Reputation: 10275

select CASE WHEN replace(Column1,'-','')= Column2 then
'Equals' else 'Not Equals' end from Table_Name

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269443

If you want to compare them, how about something like this?

select column1, column2,
       (case when column2 = replace(column1, '-', '') then 'same'
             else 'diff'
        end)
from table1;

Upvotes: 3

Related Questions