问题描述
我有以下查询,它们返回列property_value的空白值:
select target_name,target_type,property_name,property_value
from oracle_properties
where target_name like 'DY01EPI%' and property_name like 'DataGuardStatus'
sql开发人员的输出:
如果Property_value中的行为空,如何获得默认值,例如“ primary”。
我尝试使用NVL(property_value,'Primary')
和COALESCE(property_value,'Primary')
并没有得到预期的结果。请提出建议。
解决方法
大概该值不是NULL
。您可以为此使用正则表达式:
select target_name,target_type,property_name,(case when regexp_like(property_value,'[a-zA-Z0-9]') then property_value
else 'primary'
end) as property_value
from oracle_properties
where target_name like 'DY01EPI%' and
property_name like 'DataGuardStatus'
,
property_value列可能具有NULL值或该列值内所有已占用空间的空格,例如空白填充,则可以将REGEXP_REPLACE()
函数与'[[:space:]]'
模式一起使用:
SELECT target_name,NVL( REGEXP_REPLACE(property_value,'[[:space:]]'),'Primary') AS property_value
FROM oracle_properties
WHERE target_name like 'DY01EPI%'
AND property_name like 'DataGuardStatus';
为了转义所有这些空白字符。
,如何使用大小写
select target_name,case when property_value is null then 'PRIMARY' else property_value as property_value
from oracle_properties
where target_name like 'DY01EPI%' and property_name like 'DataGuardStatus'
这将根据您的要求用PRIMARY替换空值,否则它将显示实际的属性值。