oralce 空字符串自动转为null值踩坑记录

现象描述

最近接手前同事的老项目,使用oracle进行数据加工。
在一个sql之后

1
2
update table_name t set column_name = ''
where column_name = 0 ;

这一个语句之后,出现了column_name的的值为null的现象,和预期的’’空字符串预期不符合,导致后续报表展示结果不一致。。记录次坑。

问题复现

使用如下sql复现

1
2
3
4
5
6
7
8
create table test_null(
id number(10)
,test_col varchar(255)
);
insert into test_null values(1,'');
insert into test_null values(2,null);
select * from test_null;
drop table test_null;

会神奇的发现,两条结果均为null

id test_col
1 null
2 null

原因

参考stackoverflow上的答案

没啥原因,就是oracle这么设计的,将空字符串表达为null
为啥?不知道,可能太古老了。

参考资料

Why does Oracle 9i treat an empty string as NULL?
Oracle / PLSQL: Difference between an empty string and a null value