Solved

Need to know if string value has date format

Posted on 2011-03-15
4
457 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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
Comment Utility
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 73

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
oops, too slow

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

Author Comment

by:miyahira
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to recover a database from a user managed backup

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now