Rene
Rene

Reputation: 10541

How to test if date format string is a valid date format string in Oracle

I want users to be able to enter a date format string so they can specify how they want a date value to be displayed/entered.

How can I validate this date format string so that they can enter only a valid Oracle date format strings?

Upvotes: 4

Views: 25741

Answers (1)

Kevin Burton
Kevin Burton

Reputation: 11934

you could create a function:

e.g:

FUNCTION is_valid_date_format ( 
    p_format IN VARCHAR2 ) 
RETURN BOOLEAN IS
    l_date VARCHAR2(100) := NULL;
BEGIN
    l_date := TO_char( sysdate, p_format );
    RETURN TRUE;
EXCEPTION
    WHEN OTHERS THEN
        RETURN FALSE;
END is_valid_date_format;

and use it like this

IF is_valid_date_format('dd/mm/yyyy') THEN

at the moment it will allow time formats too, however it would be simple to extend it to disallow a format that contains undesired formats e.g: hh hh24 mi ss

by adding: (you will probably want to uppercase your format string first)

IF INSTR(p_format,'HH')>0 OR INSTR(p_format,'HH24')>0 
OR INSTR(p_format,'MI')>0  OR INSTR(p_format,'SS')>0 THEN
    RETURN FALSE
END IF;

Upvotes: 7

Related Questions