简体   繁体   中英

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';

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';
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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM