当 Oracle 将空字符串存储为 NULL 值时,如何处理这种情况?
我希望它被存储为一个空字符串而不是 NULL 值,因为查询会更容易。
类似于以下代码可以选择空字符串和非空字符串,但不包括 NULL 值:
select * from mytable where myfield like '%';
如果我想选择空值(原本应该是空字符串),我需要这样选择:
select * from mytable where myfield like '%' or myfield is null;
我希望能够在SQL语句中避免反复使用or myfield is null
的操作。
目前我想到的解决方案是在应用程序层面处理,例如,在实体中,我将所有字符串字段的默认值初始化为空格,例如:
@Entity
public class MyEntity {
private String name = " ";
public void setName(String name) {
if (isEmptyString(name)) {
name = " ";
}
}
...
}
或者,我可以使用Oracle 11g中我仍不知道的新类型,它可以将空字符串保持为其原样而不将其更改为null值?
谢谢!