Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two columns in SQL server

I have two columns in SQL Server in two different tables. One column has 9.011, and other table columns has 9011. I need to remove the . and compare these two columns to see whether they are equal.

Can anybody help me out how to do this?

Thanks in advance.

like image 316
user300485 Avatar asked Jan 23 '26 12:01

user300485


2 Answers

Try this:

SELECT CASE WHEN REPLACE (Table1.ColName1,'.','') = Table2.ColName2 
            THEN 'Equal' 
            ELSE 'Not Equal' 
            END AS IsEqual
    FROM Table1
    INNER JOIN Table2 ON Table1.PrimaryKey = Table2.ForeignKey

This query will return Equal if they are equal and Not Equal if they are not.

REPLACE() will remove . from ColName1.

like image 182
Raging Bull Avatar answered Jan 25 '26 01:01

Raging Bull


I am assuming that your columns are decimal type, so I have converted them to varchar first

where 
replace(Convert(varchar(50), column1 ),'.','') = Convert(varchar(50), column2)
like image 23
Raj Avatar answered Jan 25 '26 02:01

Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!