I am trying to make a Java Thread, this thread have to delete from an MySQL database all the records, older than 7 days.
In my table i have a column that contain the date like this: 2013-10-28 17:00:00 .
And to do this i want to use JDBI library. and my question here if any one could give me and example of the query that i have to write.
I think it should look like this:
h.execute("Delete from MyTable where date >= (dt.now.dayofmonth() -7)
h.execute("DELETE FROM MyTable WHERE NOW() >= ADDDATE(date, INTERVAL 7 DAY);");
Try this::
h.execute("Delete from MyTable where DATEDIFF(CURDATE(), dateCOLUMN)>7");
"delete from MyTable where date >= date_sub(now(), interval 7 day)"
Related
I want to delete the rows which are older than 2 years. In SQL I do it like this:
DELETE FROM table WHERE creation_date < (current_date - interval '2 year');
Now I want to do the same for my JPA Repository in Java with the JPQL.
In JPQL I get an error because interval is not known and the "-"(minus) is not correct.
JPQL Query
#Query("DELETE FROM table t WHERE t.creation_date < (current_date - interval '2 year')")
I would appreciate any suggestion.
You can calculate the dateTime in the application, assuming you are using LocalDateTime for creation_date
LocalDateTime dateTime = LocalDateTime.now().minus(Period.ofYears(2));
and pass the parameter in JPQL
#Query("DELETE FROM table t WHERE t.creation_date < :dateTime")
void deleteByDateTime(LocalDateTime dateTime)
I am trying to fetch 30 days back results from today in order to see who have not paid last month.
I tried something
SELECT * FROM MemberInformation WHERE DateOfAd = 'DATEADD(Day,-30,getdate())'
and query successfully executed but did not fetch the values I wanted.
My database:
please guys help me out...
DATEADD is not work in sqlite you but you can use its sqlLite equivalent DateTime like code below :
select * from TableName where DateColumn < DateTime('Now', 'LocalTime', '-10 Day');
http://www.sqlite.org/lang_datefunc.html
SQLite equivalent of SQL Server DateAdd function
Try
SELECT * FROM MemberInformation WHERE DateOfAd BETWEEN datetime('now', '-30 days') AND datetime('now', 'localtime');
or Try to change where condition as,
WHERE DateOfAd BETWEEN datetime('now', 'localtime', '-30 days') AND datetime('now', 'localtime');
I need to fetch records of last month using hql earlier i used to fetch records using sql query but now i need to fetch records of 1 month span.for example today's date is 15-Dec-2014 I want to get records between 15-Nov-2014 to 14-Dec-2014 records.
Here is Mysql query:
SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM
F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN
SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
Frankly, I don't have any idea about how to write above query in HQl.
Can anyone please help me.
Could you try this, I couldn't test, if you it has a error please inform me;
String hqlQuery =" SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN DATE_SUB(current_date(), INTERVAL 7 DAY) AND current_date()";
query = session.createSQLQuery(hqlQuery);
Initially I have a specific column to store age in my Table and I use the following to perform the select query
select count(*) from DonorDetails where age < 25
Now I have removed the age column and used dob column (with Date/Time datatype) instead. How should I write the count query which gives the same result as before ?
In the Immediate window, you can use DateSerial with a mix of other date functions to give you the date 25 years ago from today.
? DateSerial(Year(Date()) - 25, Month(Date()), Day(Date()))
2/22/1989
So in your query maybe you want something like ...
WHERE [DOB] > DateSerial(Year(Date()) - 25, Month(Date()), Day(Date()))
Here is a sample of how your SQL should look:
SELECT count(*)
FROM DonorDetails
WHERE (((DateDiff("yyyy",[dob],Date()))>25));
I need to support both, Oracle und SQL Server. I have pretty identical procedures named get_current_date in both DBs. Now I have a query:
SELECT col1, col2 FROM table WHERE colDate < get_current_date()
This works beautifully in Oracle but lacks the dbo. prefix when executed in SQL Server.
Should Spring be able to handle that? What other options do I have? I dont think it should be necessary to determine the DB type myself.
Thank you!
Every JDBC driver should support the JDBC function escapes (defined in section 13.4.1 and listed in appendix D of the JDBC 4.1 spec). For the CURRENT_DATE it is CURRENT_DATE or CURRENT_DATE().
You can call this in a query as
SELECT col1, col2 FROM table WHERE colDate < {fn CURRENT_DATE}
You can query the list of supported functions of your specific driver with DatabaseMetaData.getTimeDateFunctions()
BTW: This assumes you just want the current date of the database. It is not a general solution for calling user defined functions in your query.
Should Spring be able to handle that?
--> get_current_date() is database method/procedure (may it be of database or your custom) so it is dependent on database, Spring would not take care of that.
What other options do I have?
--> Well you can put current date in query in code so it will be independent of database what you use. Something like :
String sql = "SELECT col1, col2 FROM table WHERE colDate < " + new Date();
How about get current date in java something like:
Calendar cal = Calendar.getInstance();
currentDate = cal.getTime();
and then you do:
SELECT col1, col2 FROM table WHERE colDate < currentDate