Skip to content
Advertisement

How to evaluate a logical expression in a column in SQL

let’s say I have a database table “ACCOUNTS” where it only has 3 fields:

  1. ACCOUNT_NUMBER (BIGINT)
  2. CREDIT (BIGINT)
  3. DEBIT (BIGINT)

And I simply want to make a query that will show only two columns:

  1. ACCOUNT_NUMBER (BIGINT)
  2. “BALANCED” (A boolean value. True if Credit == Debit, false otherwise)

How do I make such a query?

I tried using the equal operator (=) but it doesn’t accept it

SELECT 
    ACCOUNT_NUMBER, 
    CREDIT = DEBIT as "BALANCED"
FROM ACCOUNTS;

Advertisement

Answer

There’s no Boolean datatype in Oracle’s SQL, so you’ll have to live with its string (or numeric) representation, such as

select account_number,
       case when credit = debit then 'true'
            else 'false'
       end as balanced
from accounts
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement