Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

sql - Return number from Oracle Select statement after parsing date

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 number in case of success :

    SELECT
     CASE
      WHEN PARSING SUCCESSFUL (ie. to_date('1-Jan-2001','dd-mon-yy') succeeds) THEN 1
      ELSE 0
     END  
    FROM DUAL;

How do I write this ? If the parsing fails, will the ELSE condition return a value ? I need to do all these checks in the SELECT statement itself. Please help.

Thanks Dileep

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 |

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...