#Query for selecting date from datetime - jpa

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

Related

Postgres OVERLAPS date function in TypeORM

I want to transfer the query to TypeORM.
It looks like this in Postgres
SELECT *
from equipment_charging
where (equipment_charging."start_date"::date, equipment_charging."end_date"::date) OVERLAPS (DATE '2020-09-10', DATE '2020-09-14')
I'm writing this query in TypeORM like this:
.where(
'(equipment_charging.startDate::date, equipment_charging.endDate::date) OVERLAPS (DATE :startDate, DATE :endDate)',
{ startDate, endDate },
)
it gives me error like this:
{"context":"ExceptionsHandler","stack":["QueryFailedError: syntax error at or near \"$1\"\n at new QueryFailedError
What am I doing wrong?
If equipment_charging.startDate and equipment_charging.endDate are already date type in database I think that you could remove ::date
In same idea try to give directly date type for the input.
I also use lot of double quote for query builder in typeorm
try this:
.where(
'("equipment_charging"."startDate", "equipment_charging"."endDate") OVERLAPS (:startDate, :endDate)',
{ startDate, endDate },
)
And convert startDate and endDate to date if needed (with parseIso of date fns for example)

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.

How to compare date in a jpql query

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.

problems to get the full DATE info from Oracle DB (dd/mm/yyyy hh/mm/ss)

I have a column in a table in which we are storing date in DATETIME format. (DD-MON-RRRR HH24:MI:SS) - Database Oracle 11g.
Data Type of a column is DATE, and storing date in 01-01-2012 01:00 PM (i.e. jan 1, 2012) format.
entity
#NotNull
#Column(name = "dateColumnName")
#Temporal(TemporalType.TIMESTAMP)
private Date sampleDate;
I am fetching all data by passing date
SAMPLE_QUERY = "select * from TableA tab where tab.dateWithTime = :sampleDate order by tab.dateWithTime ASC "
singleDate is "Tue Jan 24 00:00:00 IST 2012" , fasttime :
1327343400000
The problem is I am passing only date in the query, though Date through which records are being fetched is in DATE TIME format i.e 01-01-2012 01:00 PM.
How can i change my query so that it fetches all the records in ascending order of DateTime.
If you want to fetch all times for that day, then change your query to be more like
SELECT ... WHERE dateField >= :lowerParam AND dateField < :upperParam
Oracle has no DATE TIME datatype. The DATE datatype contains both a date and a time component, down to the second. TIMESTAMPS get a bit more complicated.
If your dateWithTime column is indeed a DATE datatype, the ORDER BY dateWithTime ASC clause should order your results in ascending order.
You may not be displaying the time component of your date. You can convert a date to a string in that format with TO_CHAR( dateWithTime, 'dd/mm/yyyy hh24/mm/ss' ) or whatever format you want.
Edit:
Oh, do you mean you want to find the cases where the date component of the DATE matches, but you don't care about the time component? That can be handled in the where clause with something like:
WHERE TRUNC( tab.dateWithTime ) = TRUNC( :sampledate )
TRUNC by default truncates a date to the beginning of the day.