How to select the comparison of two columns as one column in Oracle

后端 未结 3 2102
滥情空心
滥情空心 2020-12-14 00:35

I cannot figure out how to add a column to my SELECT query indicating whether two columns contain the same data in Oracle.

I would like to write a query like:

<
相关标签:
3条回答
  • 2020-12-14 00:44

    I stopped using DECODE several years ago because it is non-portable. Also, it is less flexible and less readable than a CASE/WHEN.

    However, there is one neat "trick" you can do with decode because of how it deals with NULL. In decode, NULL is equal to NULL. That can be exploited to tell whether two columns are different as below.

    select a, b, decode(a, b, 'true', 'false') as same
      from t;
    
         A       B  SAME
    ------  ------  -----
         1       1  true
         1       0  false
         1          false
      null    null  true  
    
    0 讨论(0)
  • 2020-12-14 00:56

    If you want to consider null values equality too, try the following

    select column1, column2, 
       case
          when column1 is NULL and column2 is NULL then 'true'  
          when column1=column2 then 'true' 
          else 'false' 
       end 
    from table;
    
    0 讨论(0)
  • 2020-12-14 01:09
    select column1, coulumn2, case when colum1=column2 then 'true' else 'false' end from table;
    

    HTH

    0 讨论(0)
提交回复
热议问题