I have two columns that are joined together on certain criteria, but I would also like to check if two other columns are identical and then return a bit field if they are.
Is there a simpler solution than using CASE WHEN?
Ideally I could just use:
SELECT Column1 = Column2 AS MyDesiredResult FROM Table1 INNER JOIN Table2 ON Table1.PrimaryKey = Table2.ForeignKey
In SQL, you can use the = operator to test for equality in a query. In this example, the SELECT statement above would return all rows from the suppliers table where the supplier_name is equal to Microsoft.
Comparison of columns in the same table is possible with the help of joins. Here we are comparing all the customers that are in the same city using the self join in SQL. Self-join is a regular join where a table is joined by itself. Similarly, a table may be joined with left join, right join, inner join, and full join.
In SQL Server, you can use the = operator to test for equality in a query. WHERE first_name = 'Jane'; In this example, the SELECT statement above would return all rows from the employees table where the first_name is equal to Jane.
If you want compare two or more columns. you must write a compound WHERE clause using logical operators Multiple-column subqueries enable you to combine duplicate WHERE conditions into a single WHERE clause.
What's wrong with CASE for this? In order to see the result, you'll need at least a byte, and that's 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With