Oracle坑之-空字符串与NULL

空字符串与NULL

首先有如下代码

SELECT *
  FROM Pdc_DataDomain DD
INNER JOIN Pdc_DD_Table DDT
    ON DD.DataDomainID = DDT.DataDomainID
   AND DD.ApplicationDBID = ‘3e7c6764d73f4c7786c99e5b72eb6912‘
   AND DDT.TableName <> ‘#‘
   AND NOT EXISTS
(SELECT *
          FROM pdc_Application_3e7c6764 A
         WHERE A.TableName = DDT.TableName
           AND A.FieldName = DDT.FieldName
           AND A.FieldType = DDT.FieldType
           AND nvl(A.FieldLength, ‘‘) = nvl(DDT.FieldLength, ‘‘)
           AND nvl(A.FieldScale, ‘‘) = nvl(DDT.FieldScale, ‘‘))

核心重点在这句AND nvl(A.FieldScale, ‘‘) = nvl(DDT.FieldScale, ‘‘)上,这句当A的FieldScale和DDT的FieldScale都为空的时候,会出现两者nvl后都返回‘‘,但是Oracle的特性是‘‘会被处理为null,而null不等于任何值,包括自己,,那么就over了,如果出现上面这种应该如何处理呢,很简单,转化的时候可以将其转化为一个程序中不会出现的值即可,如果程序逻辑没有不会出现的值,就只有使用isnull来判断了.

以下是参考资料:

-- oracle 将 空字符串即‘‘当成null,测试脚本如下:

select nvl(null,-1) from dual;
sele‘‘,‘-1‘) from dual;
select nvl(nvl(null,‘‘),-1) from dual;
select nvl(trim(‘  ‘),‘-1‘) from dual;ct nvl(

-- 但是要记住,null 与任何值做逻辑运算得结果都为 false,包括和null本身:

select nvl(max(‘1‘),-1) from dual where null = ‘‘;
select nvl(max(‘1‘),-1) from dual where null <> ‘‘;
select nvl(max(‘1‘),-1) from dual where null = ‘-1‘;
select nvl(max(‘1‘),-1) from dual where null <> ‘-1‘;
select nvl(max(‘1‘),-1) from dual where null = null;
select nvl(max(‘1‘),-1) from dual where null <> null;

-- 不过,用 is null 判断时,空字符串和 null 都 is null:

select nvl(max(‘1‘),-1) from dual where ‘‘ is null;
select nvl(max(‘1‘),-1) from dual where null is null;

-- 还要记住,null 和任何数值进行数学运算,结果都为 null:

select nvl(null + 0,-1) from dual;

-- 不过,可以使用 || 将空字符串或 null 和字符串连接

select ‘a‘ || null || ‘b‘ from dual;

-- 另,作为以下的 Oracle 内置的函数的参数时,结果也为 null:

select nvl(length(null),-1) from dual;
select nvl(trim(null),-1) from dual;
select nvl(ltrim(null),‘-1‘) from dual;
select nvl(rtrim(null),‘-1‘) from dual;
select nvl(rtrim(null,‘ ‘),‘-1‘) from dual;
select nvl(soundex(null),‘-1‘) from dual;
select nvl(SubStr(null,1),-1) from dual;
select nvl(InStr(null,1),-1) from dual;
select nvl(replace(null,‘a‘,‘b‘),‘-1‘) from dual;

select nvl(min(null),‘-1‘) from dual;
select nvl(max(null),‘-1‘) from dual;
select nvl(sum(null),‘-1‘) from dual;
select nvl(avg(null),‘-1‘) from dual;
select nvl(sum(null),‘-1‘) from dual;

-- 不过,作为以下的 Oracle 内置的函数的参数时,结果不为null:

select concat(‘a‘, null) from dual;
select concat(null, ‘a‘) from dual;

select count(null) from dual;

-- 其他函数用的时候,也可以使用以上的方法测试

来源: http://edgenhuang.iteye.com/blog/975567

Oracle坑之-空字符串与NULL,布布扣,bubuko.com

Oracle坑之-空字符串与NULL

上一篇:ORACLE_EBS_R12_采购到入库所经历的表


下一篇:Oracle Ebs R12 SLA与GL关系变化