Cree una función definida por el usuario e intente analizar la fecha; si hay excepciones, captúrelas y devuelva su cadena predeterminada.
CREATE FUNCTION check_Date (
datestring VARCHAR2,
format_mask VARCHAR2 := 'FXMMDDYYYY',
default_value VARCHAR2 := '00000000'
) RETURN VARCHAR2 DETERMINISTIC
IS
INVALID_DATE EXCEPTION;
PRAGMA EXCEPTION_INIT( INVALID_DATE, -20001 );
p_date DATE;
BEGIN
IF datestring IS NULL THEN
RAISE INVALID_DATE;
END IF;
p_date := TO_DATE( datestring, format_mask );
RETURN datestring;
EXCEPTION
WHEN OTHERS THEN
RETURN default_value;
END check_Date;
/
Alternativa :
SELECT CASE
WHEN NOT REGEXP_LIKE( datestring, '^(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{4}$' )
THEN '00000000'
WHEN TO_CHAR(
ADD_MONTHS(
TRUNC( TO_DATE( SUBSTR( datestring, 5 ), 'YYYY' ), 'YYYY' ),
TO_NUMBER( SUBSTR( datestring, 1, 2 ) ) - 1
)
+ TO_NUMBER( SUBSTR( datestring, 3, 2 ) ) - 1,
'MMDDYYYY'
)
<> datestring
THEN '00000000'
ELSE datestring
END
FROM your_table