Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL - convert select count(*) into zero or one

I'm using Oracle and I want to turn the result from a select count into a "binary" 0/1 value ... 0 = 0 ... non-zero = 1. From what I read online, in MS SQL, you can cast it to a "bit" but Oracle doesn't appear to support that.

Here's my simple example query (the real query is much more complex). I want MATCH_EXISTS to always be 0 or 1. Is this possible?

select count(*) as MATCH_EXISTS 
from MY_TABLE 
where MY_COLUMN is not null;
like image 966
krick Avatar asked Dec 05 '25 11:12

krick


1 Answers

This should be fastest... get at most one row.

SELECT COUNT(*) AS MATCH_EXISTS
FROM MY_TABLE 
WHERE MY_COLUMN IS NOT NULL
  AND rownum <= 1;
like image 151
Hogan Avatar answered Dec 07 '25 03:12

Hogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!