Home » SQL & PL/SQL » SQL & PL/SQL » Date comparison (Oracle 10.2.0.3)
Date comparison [message #627801] Mon, 17 November 2014 03:56 Go to next message
pointers
Messages: 451
Registered: May 2008
Senior Member
Hi,

I have a table "emp_details" where "doj" column is defined as NUMBER and stores date values in the yyyymmdd format (I completly oppose it, but it is an existing system)
and we have a packaged procedure which accepts input argument from emp_details.doj column and compares it with predefined date formats to find out what is the YYYY, MM and DD

for e.g. the package code does - 
    -- first comparison
	BEGIN
         SELECT TO_DATE(v_Date, 'MM/DD/YY')
           INTO v_outValDate
           FROM DUAL;
           
           RETURN TRIM(TO_CHAR(v_outValDate, 'YYYY-MM-DD'));

    EXCEPTION
       WHEN OTHERS THEN
           NULL;
    END;

	-- second comparison
      BEGIN
         SELECT TO_DATE(v_Date, 'MM.DD.YY')
           INTO v_outValDate
           FROM DUAL;

         RETURN TRIM(TO_CHAR(v_outValDate, 'YYYY-MM-DD'));

    EXCEPTION
       WHEN OTHERS THEN
           NULL;
    END;

    -- third comparison
     BEGIN
         SELECT TO_DATE(v_Date, 'DD/MM/YYYY')
           INTO v_outValDate
           FROM DUAL;

          RETURN TRIM(TO_CHAR(v_outValDate, 'YYYY-MM-DD'));

    EXCEPTION
       WHEN OTHERS THEN
           NULL;
    END;



But, for a given doj column value i.e. 20120522 (v_date above), it matches to third block of code (third comparison above) and the package returns YYYY=0522, MM=12 and DD=20 (which is completly wrong as per the application).

Now, I would like to know if we can explicitly force oracle to strictly match literal to literal, letter to letter, symbol to symbol.
Above, 20120522 should be compared to DD/MM/YYYY and should fail the comparison because there is no '/' in the input string.

Regards,
Pointers
Re: Date comparison [message #627803 is a reply to message #627801] Mon, 17 November 2014 04:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The documentation says FX can be used for that job.
Re: Date comparison [message #627804 is a reply to message #627801] Mon, 17 November 2014 04:09 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
pointers wrote on Mon, 17 November 2014 10:57

. . .
    EXCEPTION
       WHEN OTHERS THEN
           NULL;
. . .




In addition, have a look at WHEN_OTHERS
Re: Date comparison [message #627815 is a reply to message #627801] Mon, 17 November 2014 05:10 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
SELECT TO_DATE(v_Date, 'MM.DD.YY')
           INTO v_outValDate
           FROM DUAL;


No need to use SQL to just make an assignation of an expression.

Previous Topic: Error encountered while deleting a record with foreign key relationship.
Next Topic: how to find record with all 0 values records
Goto Forum:
  


Current Time: Thu Apr 25 03:33:04 CDT 2024