SQL compare two columns for same value

删除回忆录丶 提交于 2021-02-05 10:25:34

问题


With SQL Server 2012, I have two columns that I would like to compare. Both are on the same table, so no joins are needed.

Basically I need to compare two columns, for example scan1 and scan2 and if their value's match, then I need a 1, else 0. The results of the match would output to AS Results.


回答1:


Something like SELECT .... , CASE WHEN scan1 = scan2 THEN 1 ELSE 0 END AS is_equal FROM table1 should do the job.




回答2:


You can go like:

SELECT CASE WHEN Scan1 = Scan2 THEN 1 
       ELSE 0 END AS ColumnAlias
FROM YourTable


来源:https://stackoverflow.com/questions/20710825/sql-compare-two-columns-for-same-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!