MySQL DATE_ADD INTERVAL is not working with Hibernate. - mysql

My query below, shows hibernate exception:
SELECT DATE_ADD(DATE_FORMAT(MIN(t.time),'%Y-%m-%d'), INTERVAL 6 DAY) FROM Table t;
From what I understand, hibernate doesn't recognize INTERVAL keyword.
Can anyone please help me write an HQL query which gives me the same result as my above query?
(I am trying to get the date post 1 week from the minimum date in my table)

HQL and SQL are two different things. You could use a native SQL query instead of a HQL query. Or you could just execute the following query:
select min(t.time) from SomeEntity e
and add 6 days in Java:
Date minDate = (Date) query.uniqueResult();
minDate = DateUtils.addDays(d, 6); // using apache commons-lang

Related

A Relative time query which will work for both H2 and MySQL

I need to write a filter condition in SQL something of this sort -
select * from table where date > now() - INTERVAL 2 DAY
(Works in MySQL)
But this query fails in H2( Spring Boot Application). Can someone help in formulating the query which will filter date from current time stamp to 2 days before.
Tried different queries - nothing seems to work with H2.
Try DATEADD() function in H2. Alternative to NOW() in H2 is CURRENT_TIMESTAMP
select * from table where date > DATEADD('DAY',-2, CURRENT_TIMESTAMP())
You can try below using DATEADD() function
select * from table where date > DATEADD('DAY',-2, CURRENT_DATE)

how to Migrate Date_ADD method into Db2 for dynamic values

how to Migrate Date_ADD method into Db2 for dynamic values
where updatedDate BETWEEN ? AND DATE_ADD(?, Interval 1 day)
I need to run a dateadd function that will minus 1 DAY to current date
SQL has a function called dateadd, but it appears DB2 does not have this function. IS there anything equivalent to this function? If so, can someone post a syntax example?
THANKS
In DB2 you can directly add days ( eg. date + 10 days ) , months ( eg. date + 10 months) and years just like arithmetic addition.
The statement actually worked for me in DB2:
Birthdate + 52 Years as AGE 52_DATE
reference: http://www.dbforums.com/showthread.php?1637371-Help-Is-there-a-DATEADD-function-in-DB2

MySQL Query to check a DateTime column

I have a rails 3 application that is using a mysql db. Part of the functionality is that I am trying to query a table in my DB for all records where:
updated_at + 1.year <= DateTime.now
But I am having issues writing the query that would give me the expected results. Any ideas how I can go about doing this?
Try date function:
updated_at + interval 1 year <= now()
I would go with
where('updated_at <= ?', 1.year.ago)`
because it is easier to understand.

MySQL unix_timestamp calculation

I have an employees table with a field called start which is a unix_timestamp of the date the employee started. I'm calculating their most recent anniversary using a loop in PHP
$year_start = $employee['start'];
while(strtotime('+1 year', $year_start) <= time()){
$year_start = strtotime('+1 year', $year_start);
}
That works fine, but I'd like to do it as a "virtual field" in the SQL query. Anyone got a clever MySQL query that'll get the most recent anniversary in unix_timestamp?
Thanks to Barranka alerting me to the existence of date_add I think I've got a solution.
UNIX_TIMESTAMP( DATE_ADD ( FROM_UNIXTIME(start), interval +FLOOR( DATEDIFF(NOW(), FROM_UNIXTIME(start) )/365 ) year))

Compare dates in MySQL

I want to compare a date from a database that is between 2 given dates.
The column from the database is DATETIME, and I want to compare it only to the date format, not the datetime format.
SELECT * FROM `players` WHERE CONVERT(CHAR(10),us_reg_date,120) >= '2000-07-05' AND CONVERT(CHAR(10),us_reg_date,120) <= '2011-11-10'
I get this error when I execute the SQL above:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near
'us_reg_date,120) >= '2000-07-05' AND
CONVERT(CHAR(10),us_reg_date,120) <=
'2011-' at line 1
How can this problem be fixed?
You can try below query,
select * from players
where
us_reg_date between '2000-07-05'
and
DATE_ADD('2011-11-10',INTERVAL 1 DAY)
That is SQL Server syntax for converting a date to a string. In MySQL you can use the DATE function to extract the date from a datetime:
SELECT *
FROM players
WHERE DATE(us_reg_date) BETWEEN '2000-07-05' AND '2011-11-10'
But if you want to take advantage of an index on the column us_reg_date you might want to try this instead:
SELECT *
FROM players
WHERE us_reg_date >= '2000-07-05'
AND us_reg_date < '2011-11-10' + interval 1 day
This works:
select date_format(date(starttime),'%Y-%m-%d') from data
where date(starttime) >= date '2012-11-02';
Note the format string %Y-%m-%d and the format of the input date. For example 2012-11-02 instead of 12-11-2.
I got the answer.
Here is the code:
SELECT * FROM table
WHERE STR_TO_DATE(column, '%d/%m/%Y')
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
this is what it worked for me:
select * from table
where column
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
Please, note that I had to change STR_TO_DATE(column, '%d/%m/%Y') from previous solutions, as it was taking ages to load