Hibernate two date result - hql

Hibernate Between getting wrong result.If i use date 14 to 18. I am getting result 12 to 16
enter code here
** query = "Select *\n" +
"from Schedule\n" +
"Where [Date] between :fromDate and :toDate\n"**

Related

spring boot jpa mysql query performing slow

I have flow which fetches some data from the table using simple query(no joins). I triggered two requests one at t and other at t+10 minutes but both response came at same time for 1st request it took 20 mins for second request it took 10 mins. So I checked that query it is taking more time (about 20 mins) but same query I ran through mysql console taking 110 seconds. How can I improve the performance
Code to fetch data from db:
#Query(nativeQuery = true,
value = "select id, expiry_date as expiryDate "
+ "from checkout_extension "
+ "where expiry_date between :date and :nextDate",
countQuery = "select count(id) "
+ "from checkout_extension "
+ "where expiry_date between :date and :nextDate")
Page<CheckoutExpiryNotification> findByExpiryDateBetween(
final Date date, final Date nextDate, final Pageable pageable);
The same query taking very less time through mysql console. How can I improve performance?

oracle missing expression when using Max

This project requires to gather data from the pass 27 months, which will let to 2 conditions:
3 months(this year) + 12 year(previous year) + 12 years(previous previous year)
1 months(this year) + 12 year(previous year) + 12 years(previous previous year) + 2 (previous3 year) or 2 months(this year) + 12 year(previous year) + 12 years(previous previous year) + 1 (previous3 year)
I need to add a condition/column to data called 'date condition', for condition 1, only show this year and previous year's data (3 + 12 + 12) months, for condition 2, shows this year and previous year and previous previous year's data(1/2 + 12 + 12 + 1/2) months.
This condition was used to be as a calculated field in tableau, but the performance is too slow, so I want to filter it on data level. I wrote the code as below but receive error 936: missing expression
SELECT * FROM DDS_MQC_QAP_VW
WHERE (INSP_YEAR IS NULL
AND INSP_RESULT_REASON_DESC NOT LIKE '%On hold%'
AND INSP_RESULT_REASON_DESC NOT LIKE '%Aborted%'
)
OR
(
(
INSPECTION_DATE BETWEEN
ADD_MONTHS(LAST_DAY(
ADD_MONTHS( (SELECT MAX(INSPECTION_DATE) AS MAX_DATE FROM DDS_MQC_QAP_VW) -26,))+1,-1)
AND (SELECT MAX(INSPECTION_DATE) AS MAX_DATE FROM DDS_MQC_QAP_VW)
)
AND INSP_RESULT_REASON_DESC NOT LIKE '%On hold%'
AND INSP_RESULT_REASON_DESC NOT LIKE '%Aborted%'
)
related data:
Move comma to another place:
ADD_MONTHS( (SELECT MAX(INSPECTION_DATE) AS MAX_DATE FROM DDS_MQC_QAP_VW) -26,))+1,-1)
^
here
ADD_MONTHS( (SELECT MAX(INSPECTION_DATE) AS MAX_DATE FROM DDS_MQC_QAP_VW), -26))+1,-1)

Oracle Query With Date Parameter Returns No Records on Date Column

I have the following code -
CMD = new OracleCommand("SELECT * FROM BOOKINGSV3 WHERE DT = :DateInQuestion ", Con);
CMD.Parameters.Add("DateInQuestion", OracleDbType.Date).Value = DateTime.Today;
DT is a Date column, I know Oracle stores dates explicitly and if I try manually with '14-AUG-17 00:00:00' I can retrieve the desired results. How can I search by a date using a parameter other than using a string parameter and constructing a string in the format '14-AUG-17 00:00:00'?
If your DT values have non-midnight times then to get all values on a given day you can use a range:
SELECT * FROM BOOKINGSV3 WHERE DT >= :DateInQuestion AND DT < :DateInQuestion + 1
The >= means it will find times from midnight onwards; the < .. + 1 will limit to before midnight on the following day.
One way to do this is to create a string containing the date you want to search and change your query to explicitly create a date using the date format you use in your string.
CMD = new OracleCommand("SELECT * FROM BOOKINGSV3 WHERE DT = to_date(':DateInQuestion','DD-MM-YYYY HH24:MI:SS') ", Con);
This way you control the date format and not some NLS setting.

Derby Database with Date Format alfa + numeric

In storing my date in a derby table as "Apr 5 2017" (alpha + numeric) , will the query code here know how to query properly? As opposed to an all numeric format "04 05 2017". In other words, Can the query handle charvar for month sorting?
(PreparedStatement st = con.prepareStatement("select * from TEST5 where startCol >= date1 and endCol <= date2" );
ResultSet rs = st.executeQuery()) {
MainDisplay.jTable1.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
con.close();
No, if you store the date in the db as a string like this the sorting will just be a string compare and the order will not be correct (how is Derby supposed to know this is a date?).
You should define the column type as being a DATE if you want it to hold a date or TIMESTAMP for a date + time.

HQL query to filter date from datetime data

I want to convert datetime format to string format in HQL count.
For example, I have redundant attendance data in each day of month of multiple employee which I need to count and get distinct data for a single day.
select Count(distinct att.AttDate) from AttendanceTable att where att.AttDate between '" + startDate.Date + "' and '" + endDate.Date + "'
but this query counting each and every datetime data because of time value. So I need to convert datetime into string.
HQL allows only certain set of functions.
Try
select count(distinct (
day(att.AttDate) +
31 * month(att.AttDate) +
366 * year(att.AttDate) ))
You could try str() or cast() but the result won't be consistent over different databases.

Resources