此查询运行并且不返回任何行。
SELECT DISTINCT TO_CHAR(START_DATE,'MM/DD/YYYY'),
TO_CHAR(END_DATE, 'MM/DD/YYYY')
FROM
(
SELECT START_DATE, END_DATE, END_DATE - START_DATE
FROM
(
SELECT TO_DATE(SUBSTR(B, 1, 10), 'MM/DD/YYYY') START_DATE,
TO_DATE(SUBSTR(B, 14, 10), 'MM/DD/YYYY') END_DATE
FROM (SELECT 'test date' b from dual)
)
WHERE END_DATE - START_DATE != 6
)
由于转换错误,此小部分无法运行。 [1]:ORA-01858:找到了数字预期的非数字字符
SELECT TO_DATE(SUBSTR(B, 1, 10), 'MM/DD/YYYY') START_DATE,
TO_DATE(SUBSTR(B, 14, 10), 'MM/DD/YYYY') END_DATE
FROM (SELECT 'test date' b from dual)
我的期望是转换错误会导致Oracle异常导致程序失败。有些事我不知道,或者没有正确思考。
有人能指出这个方向的正确方向吗?
感谢。 恶
编辑 - 使用to_date以不同方式处理NULL。 Oracle: Avoiding NULL value in to_date
编辑 - 计划
3 SELECT STATEMENT ALL_ROWS
Cost: 3 Bytes: 0 Cardinality: 1
Partition #: 0
2 FILTER
Cost: 0 Bytes: 0 Cardinality: 0
Partition #: 0
1 FAST DUAL
Cost: 2 Bytes: 0 Cardinality: 1
Partition #: 0
编辑 - 我正在运行10g。
答案 0 :(得分:5)
SELECT START_DATE, END_DATE, END_DATE - START_DATE
FROM
(
SELECT TO_DATE(SUBSTR(B, 1, 10), 'MM/DD/YYYY') START_DATE,
TO_DATE(SUBSTR(B, 14, 10), 'MM/DD/YYYY') END_DATE
FROM (SELECT 'test date' b from dual)
)
WHERE END_DATE - START_DATE != 6
生成的执行计划(在11gR2上)如下所示:
Execution Plan
----------------------------------------------------------
Plan hash value: 4034615273
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
|* 1 | FILTER | | | | |
| 2 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_DATE(NULL)-TO_DATE('test date','MM/DD/YYYY')<>6)
因此,CBO已确定END_DATE的子字符串为空,因此,to_date也将导致空值。然后,优化器不会评估START_DATE表达式,并且不会遇到无效的数据格式错误。如果执行:
,您可以看到相同的行为SELECT TO_DATE (NULL) - TO_DATE ('test date', 'MM/DD/YYYY') FROM DUAL
它不会失败。