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

后端 未结 1 470
礼貌的吻别
礼貌的吻别 2021-01-20 19:04

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 st

相关标签:
1条回答
  • 2021-01-20 19:43

    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;
    
    0 讨论(0)
提交回复
热议问题