It would not hurt to put dates into a DATE column
Even with the view definition using a CASE
statement to conditionally cast the ldate
column, the sql statement results in errors.
- You could put a date into a
DATE
column. This is the best approach.
- Suppose you can not do the above, you could have a constraint on this column (perhaps a conditional constraint). This would probably be the next best solution.
Suppose you can not add a constraint to the table, some applications have functionality around value sets assigned to various fields (e.g. Oracle Apps descriptive flexfields fall into this category).
Suppose you might not want/can add a value set, you could have a custom utility package checking the data after the fact. This would be less than ideal.
Here is my examination of this problem:
`SCOTT@dev>CREATE TABLE schedules
2 AS
3 ( SELECT
4 hiredate ldate,
5 to_number(TO_CHAR(hiredate,'YYYYMMDD') ) ldate_number,
6 1 schtypeid
7 FROM
8 emp
9 UNION ALL
10 SELECT
11 TO_DATE(NULL),
12 to_number(NULL),
13 2
14 FROM
15 dual
16 UNION ALL
17 SELECT
18 hiredate,
19 to_number(substr(
20 TO_CHAR(hiredate,'YYYYMMDD'),
21 1,
22 4
23 ) ),
24 1
25 FROM
26 emp
27 );
Table SCHEDULES created.
SCOTT@dev>SELECT
2 COUNT(1)
3 FROM
4 schedules;
COUNT(1)
29
Create a utility package to examine/see these casting exceptions. I borrowed the logic that a number of people have used (see these links):
Justin Cave's solution: How to handle to_date exceptions in a SELECT statment to ignore those rows?
Nicholas Krasnov's solution: What exact exception to be caugth while calling TO_DATE in pl/sql code
SCOTT@dev>CREATE OR REPLACE PACKAGE util AS
2 FUNCTION to_date_exception (
3 p_char_literal IN VARCHAR2,
4 p_date_format IN VARCHAR2
5 ) RETURN VARCHAR2;
6
7 FUNCTION my_to_date (
8 p_char_literal IN VARCHAR2,
9 p_date_format IN VARCHAR2
10 ) RETURN DATE;
11
12 END;
13 /
Package UTIL compiled
SCOTT@dev>CREATE OR REPLACE PACKAGE BODY util AS
2
3 FUNCTION to_date_exception (
4 p_char_literal IN VARCHAR2,
5 p_date_format IN VARCHAR2
6 ) RETURN VARCHAR2 IS
7 l_check_date DATE;
8 l_error_code VARCHAR(20);
9 l_error_message VARCHAR2(200);
10 BEGIN
11 l_check_date := TO_DATE(p_char_literal,p_date_format);
12 -- NULL will be returned when cast works
13 RETURN NULL;
14 EXCEPTION
15 WHEN OTHERS THEN
16 l_error_code := 'ORA' || TO_CHAR(sqlcode);
17 l_error_message := sqlerrm;
18 RETURN l_error_code;
19 END;
20
21 FUNCTION my_to_date (
22 p_char_literal IN VARCHAR2,
23 p_date_format IN VARCHAR2
24 ) RETURN DATE IS
25 l_date DATE;
26 BEGIN
27 l_date := TO_DATE(p_char_literal,p_date_format);
28 RETURN l_date;
29 EXCEPTION
30 WHEN OTHERS THEN
31 RETURN TO_DATE(NULL);
32 END;
33
34 END;
35 /
Package Body UTIL compiled
SCOTT@dev>SELECT
2 util.to_date_exception(ldate_number,'YYYYMMDD') excptn,
3 util.my_to_date(ldate_number,'YYYYMMDD') the_date_all,
4 ldate
5 FROM
6 schedules
7 WHERE
8 schtypeid = 1;
EXCPTN THE_DATE_ALL LDATE
17-DEC-1980 12:00:00 AM 17-DEC-1980 12:00:00 AM
20-FEB-1981 12:00:00 AM 20-FEB-1981 12:00:00 AM
22-FEB-1981 12:00:00 AM 22-FEB-1981 12:00:00 AM
02-APR-1981 12:00:00 AM 02-APR-1981 12:00:00 AM
28-SEP-1981 12:00:00 AM 28-SEP-1981 12:00:00 AM
01-MAY-1981 12:00:00 AM 01-MAY-1981 12:00:00 AM
09-JUN-1981 12:00:00 AM 09-JUN-1981 12:00:00 AM
09-DEC-1982 12:00:00 AM 09-DEC-1982 12:00:00 AM
17-NOV-1981 12:00:00 AM 17-NOV-1981 12:00:00 AM
08-SEP-1981 12:00:00 AM 08-SEP-1981 12:00:00 AM
12-JAN-1983 12:00:00 AM 12-JAN-1983 12:00:00 AM
03-DEC-1981 12:00:00 AM 03-DEC-1981 12:00:00 AM
03-DEC-1981 12:00:00 AM 03-DEC-1981 12:00:00 AM
23-JAN-1982 12:00:00 AM 23-JAN-1982 12:00:00 AM
ORA-1840 17-DEC-1980 12:00:00 AM
ORA-1840 20-FEB-1981 12:00:00 AM
ORA-1840 22-FEB-1981 12:00:00 AM
ORA-1840 02-APR-1981 12:00:00 AM
ORA-1840 28-SEP-1981 12:00:00 AM
ORA-1840 01-MAY-1981 12:00:00 AM
ORA-1840 09-JUN-1981 12:00:00 AM
ORA-1840 09-DEC-1982 12:00:00 AM
ORA-1840 17-NOV-1981 12:00:00 AM
ORA-1840 08-SEP-1981 12:00:00 AM
ORA-1840 12-JAN-1983 12:00:00 AM
ORA-1840 03-DEC-1981 12:00:00 AM
ORA-1840 03-DEC-1981 12:00:00 AM
ORA-1840 23-JAN-1982 12:00:00 AM
28 rows selected.
Original view definition:
SCOTT@dev>CREATE OR REPLACE VIEW FOO
2 AS SELECT TO_DATE(ldate_number, 'FXYYYYMMDD') the_date,
3 ldate
4 FROM SCHEDULES
5 WHERE SCHTYPEID = 1;
View FOO created.
SCOTT@dev>SELECT
2 COUNT(1)
3 FROM
4 foo;
COUNT(1)
28
SCOTT@dev>SELECT
2 *
3 FROM
4 foo
5 WHERE
6 the_date = TO_DATE(20170918,'YYYYMMDD');
Error starting at line : 1 in command -
SELECT
*
FROM
foo
WHERE
the_date = TO_DATE(20170918,'YYYYMMDD')
Error report -
ORA-01840: input value not long enough for date format
**************************************
Proposed view definition using the CASE
statement:
SCOTT@dev>CREATE OR REPLACE VIEW FOO(THE_DATE) AS
2 SELECT CASE WHEN SCHTYPEID = 1 THEN TO_DATE(ldate_number, 'YYYYMMDD') ELSE NULL END
3 FROM SCHEDULES
4 WHERE SCHTYPEID = 1;
View FOO created.
SCOTT@dev>SELECT
2 COUNT(1)
3 FROM
4 foo;
COUNT(1)
28
SCOTT@dev>list
1 SELECT
2 the_date
3 FROM
4 foo
5 WHERE
6* the_date = TO_DATE(20170918,'YYYYMMDD')
SCOTT@dev>/
Error starting at line : 1 in command -
SELECT
the_date
FROM
foo
WHERE
the_date = TO_DATE(20170918,'YYYYMMDD')
Error report -
ORA-01840: input value not long enough for date format
As can be seen here (using Oracle 12c), I think the CASE
statement approach has issues and the underlying problem should be solved by making sure bad data does not get in to this column.