如何将空集或空值设置为默认值,如1?
到目前为止,我有这个陈述,但是如果我得到空值,我想处理它:
select case when count(*)=0
then 0
else 1
end OUTPUT
from TESTTBL
where timestamp = to_char(sysdate-1, 'yyyymmdd')||'0000';
答案 0 :(得分:5)
您的意思是检查Null值并设置为默认值,如果是这样
select nvl(column_name,'DEFAULT') from TESTBL where timestamp = to_char(sysdate-1, 'yyyymmdd')||'0000';
答案 1 :(得分:5)
SELECT CASE WHEN EXISTS
( SELECT *
FROM TESTTBL
WHERE timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'
)
THEN 1
ELSE 0
END AS OUTPUT
FROM dual
修改强>
添加FROM dual
,因为Oracle不允许SELECT
没有FROM
子句。
答案 2 :(得分:4)
你去吧
SELECT DECODE(count(*),0,0,
1) OUTPUT
FROM TESTTBL
WHERE TIMESTAMP = TO_CHAR(SYSDATE-1, 'yyyymmdd')||'0000';
使用像
这样的解码SELECT supplier_name,
decode(supplier_id, 10000, 'Google',
10001, 'Microsoft'
'Sony') result
FROM suppliers;
相当于
IF supplier_id = 10000 THEN
result := 'Google';
ELSIF supplier_id = 10001 THEN
result := 'Microsoft';
ELSE
result := 'Sony';
END IF;
或使用合并
SELECT coalesce( address1, address2) result
FROM suppliers;
相当于
IF address1 is not null THEN
result := address1;
ELSIF address2 is not null THEN
result := address2;
ELSE
result := null;
END IF;