I am using DB2 database and able to fetch data using below SQL. Here, I am looking for data
of last 10 minutes. Here CURRENT TIMESTAMP - 10 MINUTE is nothing but subtracting 10 minutes from current timestamp.
SELECT * FROM TEST WHERE param1 = '1234' AND param2 = '1' AND param3 = '0' AND DATE_TS > CURRENT TIMESTAMP - 10 MINUTE;
I am facing issue while using it with Hibernate Query Language. In Hibernate, I am able to perform the same using Native Query. But, I have to do it with HQL. How can I do it? How to reduce 10 minutes from current timestamp at SQL level.
Note: I tried in Java using reducing 10 minutes from system timestamp. But I see there TIMEZONE difference of 12Hrs. So not able to get results of last 10 minutes.
You can try this
LocalDateTime checkForDate = LocalDateTime.now().minusMinutes(10);
createQuery("SELECT * FROM TEST WHERE param1 = '1234' AND param2 = '1' AND param3 = '0' AND DATE_TS >:checkForDate").setParameter("checkForDate", java.sql.timestamp.valueOf(checkForDate));
Related
I have a column that is mapped in hibernate using org.hibernate.type.TimestampType. How can I use native Oracle SQL to store and retrieve these values based on an Oracle TIMESTAMP?
The column is mapped as NUMBER(10). I tried using some formulas I found for Unix timestamps, but they did not seem to produce the correct results.
Here is a recent timestamp:
1579730473525
This would have been some date within the last 2-3 weeks (Jan 2020).
Details
I want to use Hibernate Envers, with the ValidityAuditStrategy. I'm running into trouble because I'm applying Envers to a database of existing records that have no audit history. What I'm trying to do is do a one-time insert of audit data, using Oracle SYSDATE as the timestamp.
I wasn't sure what format your number 1579730473525 was in, so I took a guess and took the first 10 digits using a SUBSTR, and the result was a reasonable 2020 date as follows.
SELECT TO_DATE('1970-01-01','YYYY-MM-DD') +
NUMTODSINTERVAL(TO_NUMBER(SUBSTR('1579730473525',1,10)), 'SECOND') FROM dual;
returns
22/01/2020 22:01:13
Then to get the TIMESTAMP, I added the last 4 digits.
SELECT to_char(TO_DATE('1970-01-01','YYYY-MM-DD') +
NUMTODSINTERVAL(TO_NUMBER(SUBSTR('1579730473525',1,10)), 'SECOND') ,'YYYY-MM-DD hh:mm:ss') ||'.'||SUBSTR('1579730473525',10,4)
FROM dual;
and this returns:
2020-01-22 10:01:13.3525
(I couldn't find a way of directly converting the 14 digit number back to a YYYY-MM-DD hh:mm:ss:ff format; it complained of precision)
Then to get the number into TIMESTAMP format:
SELECT TO_TIMESTAMP(TO_CHAR(TO_DATE('1970-01-01','YYYY-MM-DD') +
NUMTODSINTERVAL(TO_NUMBER(SUBSTR('1579730473525',1,10)), 'SECOND') ,'YYYY-MM-DD hh:mi:ss') ||'.'||SUBSTR('1579730473525',10,4),'YYYY-MM-DD hh:mi:ss:ff')
FROM dual;
and this returns it in the Oracle TIMESTAMP as requested, using Oracle SQL.
Just a hint: if you leave out formatting, make a note of your NLS parameter NLS_TIMESTAMP_FORMAT in case you get formatting issues.
I want to select date between (System date - 2 hour ) to (system date)
I tested this for selecting records in the last two hours and it works.
#SQL Server
SELECT
*
FROM
table
WHERE
datetime_col >= DATEADD(hh, -2, CURRENT_TIMESTAMP)
AND
datetime_col <= CURRENT_TIMESTAMP;
I'm less familiar with Oracle but here is a possible solution I found online.
#oracle
SELECT
*
FROM
table
WHERE
datetime_col BETWEEN LOCALTIMESTAMP - 1/24 AND LOCALTIMESTAMP
Oracle Forum: Find record count between two time stamps
want help in sql query, I am using H2 database
i have 12 dates (date with time) in a row, field name like Date_1, Date_2, Date_3... Date_12
like to know which has max date and Time. have to know whether is there any date which is less than 10 minutes from current time.
can some one help in sql query for the above
You can use GREATEST to find max value from current row:
SELECT GREATEST(Date_1, Date_2, Date_3,... ,Date_12) AS Max_date_in_row
FROM your_table;
To find out:
is there any date which is less than 10 minutes from current time
SELECT *
FROM your_table
WHERE GREATEST(Date_1, Date_2, Date_3,... ,Date_12) > DATE_ADD(NOW(), INTERVAL -10 MINUTE);
This is my MySQL database table structure and attendancedate datatype is Date:
attendancedate-------admissionnumber------------attendance
2013-10-03-----------LSTM-0008/2013-2014--------present
2013-10-09-----------LSTM-0008/2013-2014--------present
2013-11-02-----------LSTM-0008/2013-2014--------absent
and i want to disaply like that
monthname---------totalworkingdays---------present----absent
october-------------- 2--------------------2----------0
November--------------1--------------------0-----------1
so am writing the below MySQL query:
select monthname(attendencedate) as monthname,
(select distinct count(*)
from lstms_attendence
where attendencedate>='2013-10-03'
and attendencedate<='2013-10-09'
and addmissionno='LSTM-0008/2013-2014')as totalworkingdays,
sum(CASE WHEN attendence = 'present' THEN 1 ELSE 0 END) as present,
SUM(CASE WHEN attendence = 'absent' THEN 1 ELSE 0 END) AS absent
FROM lstms_attendence
WHERE addmissionno='LSTM-0008/2013-2014' GROUP BY monthname(attendencedate);
But the query is display like this:
monthname---------totalworkingdays---------present----absent
November-----------3-----------------------0------------2
October------------3-----------------------2------------1
please give me the exact query and here am passing dates as hardcoded but that dates are passing dynamically through java to that query.
Let me know the how to create dynamically dates and how to pass values to that query.
Try this:
SELECT
MONTHNAME(attendencedate) as the_month,
COUNT(*) as working_days,
SUM(IF(attendance='present',1,0)) as is_present,
SUM(IF(attendance='absent',1,0)) as is_absent
FROM
lstms_attendence
WHERE
addmissionno='LSTM-0008/2013-2014'
GROUP BY
the_month
Your question doesn't make clear what you're asking. What's wrong with your resultset? Is it the presence of your "november" row?
At any rate, you've got an excessively complex query here, as well as one which will break if you happen to include data from multiple years in it.
Instead of using MONTHNAME(attendancedate), we'll use
DATE_FORMAT(attendancedate, '%Y-%c-01') AS attendancemonth
to figure out in which month an attendancedate lies. This simply turns '2013-11-15' into '2013-11-01', the first day of the month in question. In other words, it truncates (rounds down) the date to the first day of the month.
SELECT admissionnumber,
DATE_FORMAT(attendancedate, '%Y-%c-01') AS attendancemonth,
COUNT(*) AS working_days,
SUM(IF(attendance='present',1,0)) AS is_present,
SUM(IF(attendance='absent',1,0)) AS is_absent
FROM lstms_attendence
WHERE attendancedate >= '2013-10-03'
AND attendancedate < '2013-10-06' + INTERVAL 1 DAY
AND admissionnumber='LSTM-0008/2013-2014'
GROUP BY admissionnumber,attendancemonth
Here is a sqlfiddle.http://sqlfiddle.com/#!2/a2dfb/7/0
This summarizes by both admissionnumber and month, so you can produce a report that contains multiple admissions if you want that.
It uses attendancedate < 'last' + INTERVAL 1 DAY in place of attendancedate <= 'last' because that is robust in cases where attendancedate contains a timestamp other than midnight.
If you need to pass in your parameters from Java, use a prepared statement. Replace the constants in the SQL query with ?, and use setTimestamp and setString. For one example, see JDBC Prepared Statement . setDate(....) doesn't save the time, just the date.. How can I save the time as well?
I am trying to write an hql query which gives me the number of hours between two timestamp.
So, far i am unable to do this. I have used hql hour function but that does not work if the
timestamp corresponds to different date. Please provide any input.
My hql query is
select count(*) from com.xxx.Request as request where request.id = :id and hour(current_timestamp - request.lastEventDate) > :delay
well, you can use:
days(first_timestamp) * 24 + hours(first_timestamp)
- days(second_timestamp) * 24 - hours(second_timestamp);