Function "TO_DATE" not found in H2 database

h2, java, sql, to-date

Solution

you should be able to create your own `to_date` function

drop ALIAS if exists TO_DATE; 
CREATE ALIAS TO_DATE as '
import java.text.*;
@CODE
java.util.Date toDate(String s, String dateFormat) throws Exception { 
  return new SimpleDateFormat(dateFormat).parse(s); 
} 
' 

Of course you could also just use `parsedatetime()` per David Small's answer

Problem

I have a SQL statement and trying execute with H2 in-memory database in Java. The following exception thrown. SQL: ``` SELECT ACCT_RULE_ID, ACCT_ACTION_ID FROM ACCT_RULE WHERE (ACCT_ACTION_ID = ?) AND (START_DATETIME <= to_char(?, 'mm/dd/yyyy HH:MI:SS AM')) AND (STOP_DATETIME > to_char(?, 'mm/dd/yyyy HH:MI:SS AM')) ``` Replacing first parameter with Id and second and third parameter with new Date() value. ``` Exception: Caused by: org.h2.jdbc.JdbcSQLException: Function "TO_DATE" not found; SQL statement: ```

Original source

Related problems