ORACLE does not support not equal to empty string (<> '')

The previous application has always been connected to the DB2 database, and DB2's recognition of empty and empty strings is not equal, such as:

-- FALSE
SELECT 1 FROM DUAL WHERE '' IS NULL; 
-- TRUE
SELECT 1 FROM DUAL WHERE '' IS NOT NULL; 
-- 注意:NULL 不能用 =、!=、<> 进行比较,只能用 IS、IS NOT 进行比较

No records are returned, so when writing and filtering empty fields, both empty and empty strings should be filtered out, generally written like this

SELECT 1 FROM 表名 WHERE 字段名A IS NOT NULL AND 字段名A <> '';

Later, it was found that in fact, when writing "field name A <> 'a value'", the database has automatically filtered out the empty data, such as:

SELECT 1 FROM 表名 WHERE 字段名A <> '';

That's ok, so it's written like this when filtering empty fields later.

Recently, a point-of-use database is ORACLE, and ORACLE's identification of empty and empty strings is equivalent, that is, '' is equivalent to NULL, so that the previously written

SELECT 1 FROM 表名 WHERE 字段名A <> '';

is equivalent to

SELECT 1 FROM 表名 WHERE 字段名A <> NULL;

The NULL of ORACLE can only be compared with IS or IS NOT, but not with =, !=, <>, and the results of comparison with =, !=, <> all return FALSE, so the data has been queried all the time. come out.

 

Summarize:

In order to be compatible with DB2 and ORACLE, the SQL was finally changed to

-- 将不等于空字符串修改为不等于一个空格,也可以某个长度为 1 且该字段绝对不可能出现的任意值
SELECT 1 FROM 表名 WHERE 字段名A <> ' ';
-- 注意:
--   正常可以写随便不等于某个值,但由于 DB2 对字段数据长度的严格要求,
--   假设字段长度为 1 ,这时对比的查询条件长度为 2 (如:ab),这时查询就会报错
-- 假设“字段名B”长度为 1 ,则以下 SQL 在应用中进行查询会报错
SELECT 1 FROM 表名 WHERE 字段名B <> 'ab';
-- 而这样就不会报错了
SELECT 1 FROM 表名 WHERE 字段名B <> 'a';

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324482944&siteId=291194637