How to compare date in a jpql query - jpa

Hi I want to list all the current records in my database where the date belong to the current month.
where d.jourvisite BETWEEN :startDate AND :endDate
this is my function
public List<Visite> getvisiteDétaillé() {
Calendar MyEnddate;
MyEnddate = Calendar.getInstance();
MyEnddate.add(Calendar.MONTH, 1);
TypedQuery<Visite> query;
query =
em.createQuery("SELECT v FROM Visite v JOIN v.datevisiteList d where d.jourvisite BETWEEN :startDate AND :endDate", Visite.class);
query.setParameter("startDate",Calendar.getInstance().getTime());
query.setParameter("endDate",MyEnddate,TemporalType.DATE);
System.out.println("date actual"+Calendar.getInstance().getTime());
System.out.println("date After 30 days"+MyEnddate.getTime());
return query.getResultList();
}
I'm sure that i have some records in the DB that are suitable for the query but when I display the result nothing to display with no syntax error

In the javadoc for TemporalType.DATE they mention:
Map as java.sql.Date.
but you are passing in a Calendar for endDate parameter.
Just pass in the Date instance like you do for the startDate parameter:
query.setParameter("endDate", MyEnddate.getTime());
PS You should follow the most used naming conventions to make the code more readable.

Related

Joining a QUERY function with OR

I currently have a QUERY function which is set up based on a start date cell and an end date cell, formula as below:
=QUERY(Haulage!$A$3:$L$29," Select * Where A >= date """&text('2022 Stats'!S1, "yyyy-mm-dd")&""" AND A <= date """&text('2022 Stats'!T1, "yyyy-mm-dd")&"""")
This is working fine but I would like to adapt it so I can also narrow the query down further with the use of a dropdown. I have the following IF formula for this:
=IF('2022 Stats'!V1="All TOCs",""," AND LOWER(K) = LOWER('"&'2022 Stats'!V1&"') " )
This seems to yield the correct results but I am struggling to get the two to work together......
Link to sheet: https://docs.google.com/spreadsheets/d/1wTWuvFwMTqJ-sjIZbXWpGOS1WKwpODj2R8KAzqlqkuw/edit?usp=sharing
Try:
=QUERY(Haulage!A3:L29,
"where A >= date '"&TEXT('2022 Stats'!S1, "yyyy-mm-dd")&"'
and A <= date '"&TEXT('2022 Stats'!T1, "yyyy-mm-dd")&"'"&
IF('2022 Stats'!V1="All TOCs",,"
and lower(K) = '"&LOWER('2022 Stats'!V1)&"'"))

#Query for selecting date from datetime

I have a JPA mapped "event" object that includes a timestamp column. I am trying to select all events that occurred on a specific date. To do this I need to ignore the time component of the timestamp. I am not sure that this is possible using non database specific functions? Something like this fails to select:
#Query("SELECT r FROM BioChemRequest r WHERE r.pasId = :pasid AND r.observationDate BETWEEN :startDate AND :startDate")
public List <BioChemRequest> find(#Param("pasid") String pasid , #Param("startDate") Date startDate);
try the following
#Query("SELECT r FROM BioChemRequest r WHERE r.pasId = :pasid AND r.observationDate BETWEEN :startDate AND :endDate")
public List <BioChemRequest> find(#Param("pasid") String pasid , #Param("startDate") Calendar startDate, #Param("endDate") Calendar endDate);
where startDate is a calendar with hour 00:00:00 000
and endDate is a calendar with hour 23:59:59 999

Is there a way to use java LocalDateTime.now() in jpa #query? [duplicate]

I want to compare the date in database with current dateTime in JPA query :
captureLimitDate < currentDateTime
my requirement is as follows :
database.captureLimitDate : 04/07/2012 19:03:00
currentDateTime : 04/07/2012 20:03:00
My JPAQuery is this :
SELECT o FROM Operation o"
+ " WHERE ( o.merchantId =:merchantId ) AND "
+ "(o.captureLimitDate < currentDateTime ) ";
And Operation class has captureLimitDate as java.util.Date
#Generated(value = "XA", comments = "0,_8BedAMXZEeGHf_Dj4YaPyg")
private Date captureLimitDate;
I want to compare both current date and time . will the above query works. ??
CURRENT_TIMESTAMP must be used to refere to the current date and time in a JPQL query:
select o from Operation o
where o.merchantId = :merchantId
and o.captureLimitDate < CURRENT_TIMESTAMP
If the current date and time is in fact a date coming from user input (and which is thus not the current date and time, then you do it like you do for the merchantId:
select o from Operation o
where o.merchantId = :merchantId
and o.captureLimitDate < :maxDateTime
And you set the parameter using
query.setParameter("maxDateTime", maxDateTime, TemporalType.TIMESTAMP);
JPA defines special JPQL expressions that are evaluated to the date and time on the database server when the query is executed:
CURRENT_DATE - is evaluated to the current date (a java.sql.Date instance).
CURRENT_TIME - is evaluated to the current time (a java.sql.Time instance).
CURRENT_TIMESTAMP - is evaluated to the current timestamp, i.e. date and time
(a java.sql.Timestamp instance).
More info: Click here

CrudRepository,Can't get the correct result query with And Between

I am having trouble using spring-boot CrudRepository,What I want to do is to look up the data for a period of time
I make a query by this and result is ok
List<T> findByTimestampAfter(#Param("timestamp") Date start)
This is also ok
List<T> findByTimestampBefore(#Param("timestamp") Date end)
But I tried the following three methods do not work:
List<T> findByTimestampBetween(#Param("timestamp") Date start,#Param("timestamp") Date end)
List<T> findByTimestampAfterAndTimestampBefore(#Param("timestamp") Date start,#Param("timestamp") Date end)
List<T> findByTimestampGreaterThanEqualAndTimestampLessThanEqual(#Param("timestamp") Date start,#Param("timestamp") Date end)
What I want to get is more than 100 data in the time frame, but there is only one which timestamp euqal end
And i try execute this sql statementselect:
select * from exchange where timestamp > "2018-01-02 17:30:00" AND timestamp < "2018-01-02 17:48:00";
i can see the more than 100 data.
I think I can not solve this problem
The keyword between is not supported inside method name. You should define your custom query, for example:
#Query("select * from exchange where timestamp > ?1 AND timestamp < ?2")
Object[] findByTimeCustomQuery(Date dFrom, Date dTo);
See the query executing from the jpa automatically.If you just use proper debugging logger .Also tell the proper result achieved from above three methods.

database query with start and end date

I do a select and its working perfect but I want to change it to a select with a from and to date, how would I change my sample to do this? I will get the date from a richfaces calender,
I have a filter on my page that selects all the values for the sql select. so I want to a start and end date to that filter and only get the results between those dates. only "dd/MM/yyyy" can be used as its not necessary for the time
example of select:
myList = database.createQuery("SELECT t FROM Logging t WHERE t.No = :No and t.customer = :customer and t.project = :project and t.task = :tasks").setParameter("No", getNo()).setParameter("customer", getCustomer()).setParameter("project", getProject()).setParameter("task", getTask()).getResultList();
This resolved it for me: and t.date BETWEEN :startDate AND :endDate at the end and then just added the setParameter with the values I get from richfaces calender and works perfect!