jdamae
jdamae

Reputation: 3909

how to set a null value to default in oracle sql

How do I set an empty set or null value to a default value like 1?

So far, I have this statement, but in case I get null values i want to handle that:

select case when count(*)=0 
                   then 0 
                 else 1 
                   end OUTPUT 
from TESTTBL 
where timestamp = to_char(sysdate-1, 'yyyymmdd')||'0000';

Upvotes: 2

Views: 11457

Answers (3)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115530

SELECT CASE WHEN EXISTS
                   ( SELECT * 
                     FROM TESTTBL 
                     WHERE timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
                   )
            THEN 1
            ELSE 0
       END AS OUTPUT
FROM dual                     

EDIT

Added FROM dual as Oracle does not allow SELECT without FROM clause.

Upvotes: 5

Mangesh
Mangesh

Reputation: 3997

Here you go

SELECT DECODE(count(*),0,0,
                      1) OUTPUT
   FROM TESTTBL 
   WHERE TIMESTAMP = TO_CHAR(SYSDATE-1, 'yyyymmdd')||'0000'; 

Use Decode like

SELECT supplier_name,
        decode(supplier_id, 10000,  'Google',
                            10001,  'Microsoft'                                
                           'Sony') result
 FROM suppliers;

equivalent to

IF supplier_id = 10000 THEN
     result := 'Google';

ELSIF supplier_id = 10001 THEN
    result := 'Microsoft';

ELSE
    result := 'Sony';    
END IF;

Or Use coalesce

SELECT coalesce( address1, address2) result
FROM suppliers;

which is equivalent to

IF address1 is not null THEN
     result := address1;

ELSIF address2 is not null THEN
    result := address2;

ELSE
    result := null;

END IF;

Upvotes: 4

user747858
user747858

Reputation:

Do you mean to check for Null value and set as some default, if so

select nvl(column_name,'DEFAULT') from TESTBL where timestamp = to_char(sysdate-1,   'yyyymmdd')||'0000';

Upvotes: 6

Related Questions