Solved

Need to know if string value has date format

Posted on 2011-03-15
4
460 Views
Last Modified: 2013-12-19
Hello,
I'm trying to know if a string value has date format. I use a function Is_Date to get that result, but no success.

select is_date('01/02/90xxx','DD/MM/YYYY HH24MISS') from dual;

Get error: ORA-01830: date format picture ends before converting entire input string


FUNCTION is_date (p_inDate DATE, p_format VARCHAR2)
      RETURN NUMBER
   AS
      v_dummy   DATE;
   BEGIN
      BEGIN

      SELECT TO_DATE (p_inDate, p_format) INTO v_dummy FROM DUAL;

      RETURN 1;
      EXCEPTION
         WHEN VALUE_ERROR
         THEN
            v_dummy := NULL;
      RETURN 0;
      END;

      RETURN v_dummy;
   END is_date;
0
Comment
Question by:miyahira
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 35139120
If the input parameter is a DATE, it must be valid.

Check out:
create or replace FUNCTION is_date (p_inDate varchar2, p_format VARCHAR2)
      RETURN NUMBER
   AS
      v_dummy   DATE;
   BEGIN
      BEGIN

      v_dummy := TO_DATE (p_inDate, p_format);

      RETURN 1;
      EXCEPTION
      when others then
            RETURN 0;
      END;
END is_date;
/

show errors



select is_date('01/02/90xxx','DD/MM/YYYY HH24MISS') from dual;
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 35139148
try something like this  ( I fixed some syntax errors too)


CREATE OR REPLACE FUNCTION is_date(p_indate VARCHAR2, p_format VARCHAR2)
    RETURN NUMBER
AS
    v_dummy   DATE;
BEGIN
    v_dummy  := TO_DATE(p_indate, p_format);

    RETURN 1;
EXCEPTION
    WHEN OTHERS
    THEN
        RETURN 0;
END is_date;
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35139158
oops, too slow

same idea, but I removed the extra begin/end block which wasn't needed
0
 
LVL 1

Author Comment

by:miyahira
ID: 35139237
Thanks to both of you. I was blind, didn't see that I declared parameter p_indate as DATE and should be VARCHAR2
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question