Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check time format in Oracle

I need to check that my string is in a format of time HH24:MI:SS.

I made a function

CREATE OR REPLACE FUNCTION check_time(myStr VARCHAR2) RETURN INT IS
   p_temp DATE;
BEGIN
     p_temp := TO_DATE(myStr, 'HH24:MI:SS');    
   RETURN 1;
 EXCEPTION WHEN others THEN RETURN 0;
END;

But the TO_DATE() not failing when getting number between 0 and 23;
I need the string to be in the exact format of HH24:MI:SS with the colon.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I looking for a solution that uses the Oracle Date/Time formats.

>Solution :

If you want to prevent Oracle from apply lax rules to the conversion then you can add the ‘FX’ format modifier:

p_temp := TO_DATE(myStr, 'FXHH24:MI:SS');

On recent versions of Oracle you don’t need your own function, you can use validate_conversion(), which will give you the same 0/1 result:

validate_conversion('<your_string>' as date, 'FXHH24:MI:SS')

fiddle with some sample valid and invalid values.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading