Return number from Oracle Select statement after parsing date

前端 未结 1 1543
说谎
说谎 2020-12-04 04:27

I want to write a Oracle SQL select statement that tells if it could parse a date string in the given format by returning a code - zero on failure (exception) and a positive

相关标签:
1条回答
  • 2020-12-04 05:03

    If you can create a function then you can do something like:

    SQL Fiddle

    Oracle 11g R2 Schema Setup:

    CREATE OR REPLACE FUNCTION is_Valid_Date (
      p_date   IN VARCHAR2,
      p_format IN VARCHAR2 DEFAULT 'DD-MON-YY'
    ) RETURN NUMBER
    IS
      d DATE;
    BEGIN
      d := TO_DATE( p_date, p_format );
      RETURN 1;
    EXCEPTION
      WHEN OTHERS THEN
        RETURN 0;
    END;
    /
    

    Query 1:

    SELECT is_Valid_Date( '12-Feb-13' ),
           is_Valid_Date( 'XX-Feb-13' )
    FROM DUAL
    

    Results:

    | IS_VALID_DATE('12-FEB-13') | IS_VALID_DATE('XX-FEB-13') |
    |----------------------------|----------------------------|
    |                          1 |                          0 |
    
    0 讨论(0)
提交回复
热议问题