我的查询有问题。请告诉我克服这个问题。
SELECT COUNT(*)
FROM TABLE_NAME
WHERE FIELD = '2'
AND FILED2 = '0B'
AND TRIM(LEADING 0 FROM FILED3) = TRIM(LEADING 0 FROM '00');
FIELD3
是4个字符大小,在表格中保存为00
。因此条件变为null = null
,返回false
。
这有什么替代品吗?
答案 0 :(得分:0)
Oracle将空字符串视为NULL
。使用IS NULL
进行比较:
SELECT COUNT(*)
FROM TABLE_NAME
WHERE FIELD = '2'
AND FILED2 = '0B'
AND TRIM(LEADING 0 FROM FILED3) IS NULL;
答案 1 :(得分:0)
我通常使用以下内容来避免空比较:
SELECT COUNT(*)
FROM TABLE_NAME
WHERE FIELD = '2'
AND FILED2 = '0B'
AND NVL(TRIM(LEADING 0 FROM FILED3), 'NULL') = NVL(TRIM(LEADING 0 FROM '00'), 'NULL');
基本上当存在null时,我将其转换为字符串,因此可以进行比较。
如果您要比较不同的值,这非常有用。但是如果你总是比较NULL,那么使用IS NULL
SELECT COUNT(*)
FROM TABLE_NAME
WHERE FIELD = '2'
AND FILED2 = '0B'
AND TRIM(LEADING 0 FROM FILED3) IS NULL;