oracle中的空字符串

Fer*_*ito 0 sql oracle

当我在数据库表列中插入一个空字符串时,它看起来像是在内部存储为NULL. 那么为什么我不能通过精确查找那个空字符串来选择相应的行呢?

例子:

insert into mytable (mycolumn,col2,col3) values ('','xyz','abc');

// now there is a row having mycolumn == NULL

select * from mytable where mycolumn='';

// empty :(
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

Gor*_*off 5

这是 Oracle 中的一个奇怪的时代错误(使用默认设置)。Oracle 确实将空字符串视为NULL. 这包括比较,所以:

where mycolumn = ''
Run Code Online (Sandbox Code Playgroud)

是相同的:

where mycolumn = NULL
Run Code Online (Sandbox Code Playgroud)

这永远不会返回 true ( NULL <> NULL)。

我的建议?习惯于NULL显式使用和编写:

where mycolumn is null
Run Code Online (Sandbox Code Playgroud)