How can i use this sql query to hibernate criteria select * from DailyExpense where month(tdate)=07 and year(tdate)=2016 Daily expense is my pojo class and tdate is column name. i want to select current month data only how to select based on month only.
Use HQL.
from DailyExpense where year(birthDate)=:year and month(birthDate)=:month"
Afterwards, bind :year and :month parameters using query's setString(whetever value of your month and year)
Related
I am trying to create JPA specification for with filter data between two dates date1 and date2, I tried to create through subquery but no luck as of now, please help if possible.
I am trying to create the specification for the below SQL query,
select
* from
subscription sub join (
select subscription_id,
max(output_date) as max_output_date
from
subscription_package
where
package_status = 'COMPLETE'
group by
subscription_id) sp on
sub.id = sp.subscription_id where
max_output_date between 'date1' and 'date2'
Below is the query created in JPA:
Subquery<Date> datequery = query
.subquery(Date.class);
Root<SubscriptionPackage> dateRoot = datequery
.from(SubscriptionPackage.class);
Subquery<SubscriptionPackage> subquery = query
.subquery(SubscriptionPackage.class);
Root<SubscriptionPackage> subqueryRoot = subquery
.from(SubscriptionPackage.class);
datequery.select(criteriaBuilder
.greatest(subqueryRoot.<Date>get("outputDate")));
datequery.where(criteriaBuilder.equal(subqueryRoot.get("PackageStatus"),
'COMPLETE'));
datequery.groupBy(subqueryRoot.get("subscription"));
not able to use the result from date query for the comparison like
query.where(criteriaBuilder.between(datequery., outFrom, outTo));
I don't know why you are using Subquery and you can use simple Spring data jpa with joins get your desire result.
Please check these examples
For date comparison:
criteriaBuilder.between(root.get("createdDate"), fromDate, toDate));
id name date
1. ask 2018-04-25 12:30:59
2. msk 2018-04-25 12:40:43
3. sdf 2017-05-25 12:42:34
id=int---->in java id-->int
name=varchar(25)----> in java name-->string
date=datetime------->in java date--->Timestamp
my sql query=select * from table where year(date)='2018';
o/p:1. ask 2018-04-25 12:30:59
2. msk 2018-04-25 12:40:43
select * from table where month(date)='05'
o/p:3. sdf 2017-05-25 12:42:34
please help me i dont know
how to write this query in hibernate
How to write the above queries in hibernate? I have tried lot things but I didn't get any solution. If I use to_char() in hibernate it will give a unexpected token error.
You do not have to use to_char() function. Hibernate supports year and date functions.
Refer this link: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html
and look under "14.10. Expressions".
So your hql would simply look like this:
select t from table t where YEAR(t.date)='2018';
and select t from table t where MONTH(t.date)='05';
there is no need to use to_char() method here. you can simply use this query inside your Dao Implementation class.
for years:
session.createSQLQuery("select * from table where year(date) = :year")
.setParameter("year",2018)
.list();
for Months
session.createSQLQuery("select * from table where month(date) = :month")
.setParameter("month",05)
.list();
I have a table and in that table there is a column called date_created, which is in TIME STAMP format 2016-07-20 11:30:15 and while I am fetching record I have to pass only the month and year in my front-end field like (month_no/year) 1/2016, 2/2016, 3/2016..... So I have to fetch all the record based upon a month. So how could I achieve this query. So far I tried like this.
SELECT t.*
FROM stock t
WHERE MONTH(str_to_date(t.date_created,'%Y/%M')) = 7
Since you have a timestamp column you don't need to worry too much. You need to put the month number and year number in the corresponding position.
SELECT t.*
FROM stock t
WHERE MONTH(t.date_created) = ?
AND YEAR(t.date_created) = ?;
If month = 7 and year = 2016 then your query:
SELECT t.*
FROM stock t
WHERE MONTH(t.date_created) = 7
AND YEAR(t.date_created) = 2016;
If You have created indexes on columns then i would suggest you not use any function in where clause this makes query slow and non-sargable.. Please use the following link why i am saying this
What makes a SQL statement sargable?
I would Suggest you to use the following query.
SELECT t.*
FROM stock t
WHERE t.date_created between '2016-07-01 00:00:00.000' AND '2016-07-31 00:00:00.000'
I've created a program using Java that connects to a database and allows the user to submit a record. This record includes a unique reference number and started date (Variable: StartedDate).
I want to enable the user to search for the amount of cases submitted between two dates (the first day of the month and todays date).
So far I have the SQL query
select * from cases where StartDate>'***' and Date<'****'
In Java, it would be
select * from cases where StartedDate>'1stMONTHDATE' and Date<'TODAYSDATE'
My first question is - Within the database, there isn't a field called "Date" as this would have to change on a daily basis. How would I reference this to be the automatically generated date from Java?
Also, how would I implement the Count() method to return an int of the number of records returned?
SELECT * FROM cases WHERE StartedDate BETWEEN :startDate AND :endDate
In JPQL:
public List<Cases> findAllEvents(Date startDate, Date endDate) {
List<Cases> allCases = entityManager.createQuery("SELECT * FROM cases WHERE StartedDate BETWEEN :startDate AND :endDate")
.setParameter("startDate", startDate, TemporalType.DATE)
.setParameter("endDate", endDate, TemporalType.DATE)
.getResultList();
return allCases ;
}
Well i suppose by "Date" in the query you mean end date.
if that is so you can calculate the end date with either of the two following methods
java using the Calender class and pass the end date as parameter to the query
In the sql by modifying the query like
for ex- here the end date is one month from start date.This is DB specific.This will work in Postgres. Look for the correct syntax according to the DB you use to get the end date
select * from cases where StartedDate>'1stMONTHDATE' and ('StartedDate' + INTERVAL '1 MONTH')<'TODAYSDATE'.
Regarding the count()-A simple SELECT Count(*) will give you the number of rows returned by thw query
If you are using MSSQL and your date values are as String then you can use following query:
SELECT c FROM cases c where CONVERT(VARCHAR(10),StartDate,121) between :startDate AND :endDate
I am using this query in my application with mssql server
In SQL I have a query like this..
SELECT * FROM userdata where date_format(startdate,'%p') = 'AM'
How can I change this query to HQL? For a data range I need only morning data.
date_format should work in hql as well. Try the same query in hql.