Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert special String into Date in H2

There is a SQL function from Oracle to_date('26 Jul 2016, 05:15:58 AM','DD Mon YYYY, HH:MI:SS AM'), and it throws exception "Illegal pattern character 'o'" in H2.

How shall I change it to make it work in H2?

like image 687
Iceglaze Avatar asked Jul 26 '16 09:07

Iceglaze


People also ask

How do I convert a string to a date in SQL?

In SQL Server, converting a string to date explicitly can be achieved using CONVERT(). CAST() and PARSE() functions.

Which function can be used to convert string to date?

MySQL STR_TO_DATE() Function The STR_TO_DATE() function returns a date based on a string and a format.

What is the date format in H2 database?

The format is hh:mm:ss[. nnnnnnnnn].

How do you cast a date?

The CAST() function in MySQL is used to convert a value from one data type to another data type specified in the expression. It is mostly used with WHERE, HAVING, and JOIN clauses. This function is similar to the CONVERT() function in MySQL. It converts the value into DATE datatype in the "YYYY-MM-DD" format.


1 Answers

The equivalent function of TO_DATE() in H2 is PARSEDATETIME().

This is how you should use it with your sample data:

PARSEDATETIME('26 Jul 2016, 05:15:58 AM','dd MMM yyyy, hh:mm:ss a','en')

Be careful not to use HH:mm:ss otherwise the AM/PM detection will not work.

like image 144
Christian MICHON Avatar answered Sep 29 '22 07:09

Christian MICHON