Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATE_SUB and DATE_ADD in H2 for MySQL

Tags:

mysql

scala

h2

I am using MySQL as my database and H2 for testing. I am also using playframework 2.3.x and Scala, but I think does not matter for the question purpose.

H2 has a conflict with some functions that I am using in a query

SELECT *
FROM subscriptions
WHERE active_until >= (DATE_SUB(CURDATE(), INTERVAL 3 DAY))
AND active_until <= (DATE_ADD(CURDATE(), INTERVAL 1 DAY))
AND status = "ACTIVE"

The functions that cause the problem are DATE_SUB and DATE_ADD.

Is there a workaround where I can make this work or change the query without breaking it for mysql?

like image 972
agusgambina Avatar asked May 18 '16 15:05

agusgambina


People also ask

What is DATE_SUB in MySQL?

The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.

What is Date_add in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.

Does H2 support MySQL?

SQL SupportCompatibility modes for IBM DB2, Apache Derby, HSQLDB, MS SQL Server, MySQL, Oracle, and PostgreSQL.

How do I subtract one day from a date in MySQL?

In MySQL, you can subtract any date interval using the DATE_SUB() function. Here, since you need to subtract one day, you use DATE_SUB(CURDATE(), INTERVAL 1 DAY) to get yesterday's date.


1 Answers

Finally I was able to solve it. I had to change DATE_ADD for TIMESTAMPADD and DATE_SUB with TIMESTAMPDIFF. Then I changed CURDATE() for CURRENT_DATE. Also the sign of the method change but works on both H2 and My.

SELECT *
FROM subscriptions
WHERE active_until >= (TIMESTAMPDIFF(DAY, 3, CURRENT_DATE))
AND active_until <= (TIMESTAMPADD(DAY, 1, CURRENT_DATE))
AND status LIKE 'ACTIVE'
like image 98
agusgambina Avatar answered Sep 17 '22 14:09

agusgambina