hql query to get last 1 month records - java

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);

Related

Delete records when the difference in days between two dates is greater than a certain value in java

I need to delete records when the difference between SYSDATE and insertDate(field of my table with timestamp(6) format) is greater than 20 days.
I tried the following:
String myQuery = "delete from STUDENT where SYSDATE - insertDate > INTERVAL '20' DAY";
session.createQuery(myQuery).executeUpdate();
but I get this error:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 20 near line 1
after this query I create a Criteria to retrieve the list of remaining students. is this the best way to delete records? or should i use Criteria? if so how? thanks
Try this query:
String myQuery = "delete from STUDENT where (SYSDATE - insertDate) > 20";
session.createQuery(myQuery).executeUpdate();

I want to fetch 30 days back results from sqlite DB

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');

Selecting the records with maximum date value

String sql1="SELECT MAX(date),epf_rte_emp,epf_rte_com,etf_rte FROM Fixed_Rates1";
I'm using this query to select the record with the maximum date value, but it gives the latest updated record. I want the record with the maximum date.This query gives the record with maximum date in the SQLiteManager, but doesn't gives the required output in netbeans. Could someone please help me in this?
If you want one record, then something like this shoudl work:
select fr.*
from fixed_rates1 fr
order by date desc
limit 1;
Note: I am guessing you are using MySQL, because your query would fail in most other databases. The method for limiting results depends on the database.
If you want all rows with the maximum date:
select fr.*
from fixed_rates1 fr
where fr.date = (select max(fr2.date) from fixed_rates1 fr2);

Hibernate Query search criteria filter by timestamp

Good day,
I have a db2 database, and I have a sql query as follow:
select * FROM SCORPORATEREGISTEREDACCOUNTS CRA WHERE cra.corporateRegisteredAccountId = 241
and cra.paymentDate >= '2017-07-07 00:00:00' and cra.paymentDate <= '2017-07-07 23:59:59';
And in my database, the row record having paymentDate = '2017-07-07 11:48:00'. This paymentDate column is set as TIMESTAMP for its data type.
When I run this query through command, or through dbVisualizer, I can get the result correctly.
However, When run this query through java code using Hibernate Query, I will have some weird result, which is getting null record for the first time, and have record on the second time and onward, and If I log out from my java web application, and log in again, same problem occur.
Here is my sample code that trigger by a search button:
StringBuilder sb = new StringBuilder( );
sb.append( " \\ my query here " );
Query query = getSession( ).createSQLQuery( sb.toString( ) );
return query.list();
In other word, I have to press the search button second time only can get the correct result.
And here is some of my test case:
If I change the row record paymentDate to '2017-07-07 00:00:00', then it wont have issue for searching.
If I change my sql query to search within 2 day instead of 1 day, then it wont have issue also.
where cra.paymentDate >= '2017-07-07 00:00:00' and cra.paymentDate <= '2017-07-07 23:59:59'
Or I change my code to call the query.list() 2 times (only return at second time):
query.list();
return query.list();
However, my solution is cast the column to date:
where date(cra.paymentDate) >= '2017-07-07 00:00:00' and date(cra.paymentDate) <= '2017-07-07 23:59:59'
My question is, how come the original query wont work for first time, and only work for second time. If think as logic, it should fulfill the search criteria and return me correct result. Is it a Hibernate Query bugs? Or there is other reason or something wrong with my code.
Kindly advise.

Get data for specific month from MS Access 2016 db

Good day, I am struggling with an sql select query from my java application. My application connects with an MS Access database. I would like to retrieve all records logged during a specific month. This is my statement:
Select * from tbl q
Join (
Select s.Customer from tbl s
Where Month(s.LogDate) = 1 And Year(s.LogDate) = 2017);
Problem is that my resultset returns data logged for both January 2017 and December 2016. I have tried different approaches one of which was to pass an sql date(#date#) but I still get the same result. What am I doing wrong?
You shouldn't need the join or the subquery. Try
Select * from tbl where month(logdate)=1 and year(logdate)=2017

Categories

Resources