[英]Replace blank value with a default value in SQL
我有以下查询,它返回列 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, target_type, property_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,target_type,property_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 替换空值,否则它将显示实际的属性值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.