How do I compare two columns for equality in SQL Server?

How do I compare two columns for equality in SQL Server?

Whats wrong with CASE for this? In order to see the result, youll need at least a byte, and thats what you get with a single character.

CASE WHEN COLUMN1 = COLUMN2 THEN 1 ELSE 0 END AS MyDesiredResult

should work fine, and for all intents and purposes accomplishes the same thing as using a bit field.

CASE WHEN is the better option

SELECT 
  CASE WHEN COLUMN1 = COLUMN2 
    THEN 1 
    ELSE 0 
  END 
  AS MyDesiredResult
FROM Table1
INNER JOIN Table2 ON Table1.PrimaryKey = Table2.ForeignKey

How do I compare two columns for equality in SQL Server?

The use of IIF? And it depends on version of SQL Server.

SELECT
IIF(Column1 = Column2, 1, 0) AS MyDesiredResult
FROM Table;

Leave a Reply

Your email address will not be published.