1

I am currently stuck with this approch not getting how do i need to minus with current timestamp and find all records which as a difference of 5 minutes or less

My table

create table demo ( id number, modified_date timestamp(6) );

insert into demo values(1,to_timestamp('10-MAY-19 07.14.15.066000000 AM','DD-MON-RR HH.MI.SSXFF AM'));   
insert into demo values(5,to_timestamp('10-MAY-19 07.16.11.064000000 AM','DD-MON-RR HH.MI.SSXFF AM'));    
insert into demo values(3,to_timestamp('10-MAY-19 07.15.13.062000000 AM','DD-MON-RR HH.MI.SSXFF AM'));
insert into demo values(12,to_timestamp('10-MAY-19 07.18.10.056000000 AM','DD-MON-RR HH.MI.SSXFF AM'));
insert into demo values(14,to_timestamp('10-MAY-19 07.20.09.046000000 AM','DD-MON-RR HH.MI.SSXFF AM'));

Data in table

id  modified_date 
1   10-MAY-19 07.14.15.066000000 AM
5   10-MAY-19 07.16.11.064000000 AM
3   10-MAY-19 07.15.13.062000000 AM
12  10-MAY-19 07.18.10.056000000 AM
14  10-MAY-19 07.20.09.046000000 AM

My query does not give in minutes

select modified_date - current_timestamp as dt_minutes from demo;

Expected : Need record as a difference of 5 minutes or less with current_timestamp

id  modified_date                    date_differenence    
1   10-MAY-19 07.14.15.066000000 AM  modified_date - current_timestamp ( in minutes )
5   10-MAY-19 07.16.11.064000000 AM  modified_date - current_timestamp ( in minutes )
3   10-MAY-19 07.15.13.062000000 AM  modified_date - current_timestamp ( in minutes )
12  10-MAY-19 07.18.10.056000000 AM  modified_date - current_timestamp ( in minutes )
14  10-MAY-19 07.20.09.046000000 AM  modified_date - current_timestamp ( in minutes )

Note:

NLS_LANGUAGE = 'AMERICAN'
NLS_TIMESTAMP_TZ_FORMAT = 'DD-MON-RR HH.MI.SSXFF AM TZR'
NLS_CALENDAR ='GREGORIAN'
1
  • The sample data in your question is all from May 2019; this month is July 2024 and none of your rows are going to be within 5 minutes of the current date (they aren't even within 5 years). Please explain, in English (not code), what the logic is that you are expecting to implement and what is the expected output for your sample data?
    – MT0
    Commented Jul 7 at 14:00

2 Answers 2

1

Use an INTERVAL DAY TO SECOND:

SELECT id,
       modified_date,
       current_date,
       modified_date - current_timestamp AS interval_difference,
       EXTRACT(MINUTE FROM modified_date - current_timestamp) AS minutes_difference
FROM   demo
WHERE  modified_date BETWEEN CURRENT_TIMESTAMP - INTERVAL '5' MINUTE
                         AND CURRENT_TIMESTAMP + INTERVAL '5' MINUTE;

Use ABS(EXTRACT(MINUTE FROM current_date - modified_date)) if you are not worried about the sign.

Which, for the sample data:

CREATE TABLE demo (
  id NUMBER,
  modified_date TIMESTAMP(6)
);

INSERT INTO demo (id, modified_date)
  SELECT 1, CURRENT_TIMESTAMP + INTERVAL '10' MINUTE FROM DUAL UNION ALL
  SELECT 2, CURRENT_TIMESTAMP + INTERVAL '5:01' MINUTE TO SECOND FROM DUAL UNION ALL
  SELECT 3, CURRENT_TIMESTAMP + INTERVAL '5:00' MINUTE TO SECOND FROM DUAL UNION ALL
  SELECT 4, CURRENT_TIMESTAMP + INTERVAL '4:59' MINUTE TO SECOND FROM DUAL UNION ALL
  SELECT 5, CURRENT_TIMESTAMP + INTERVAL '0' MINUTE FROM DUAL UNION ALL
  SELECT 6, CURRENT_TIMESTAMP - INTERVAL '4:59' MINUTE TO SECOND FROM DUAL UNION ALL
  SELECT 7, CURRENT_TIMESTAMP - INTERVAL '5:01' MINUTE TO SECOND FROM DUAL UNION ALL
  SELECT 8, CURRENT_TIMESTAMP - INTERVAL '5' YEAR FROM DUAL;

Outputs:

ID MODIFIED_DATE CURRENT_DATE INTERVAL_DIFFERENCE MINUTES_DIFFERENCE
3 2024-07-07 15:42:27.262701 2024-07-07 15:37:27 +000000000 00:04:59.975026 4
4 2024-07-07 15:42:26.262701 2024-07-07 15:37:27 +000000000 00:04:58.975026 4
5 2024-07-07 15:37:27.262701 2024-07-07 15:37:27 -000000000 00:00:00.024974 0
6 2024-07-07 15:32:28.262701 2024-07-07 15:37:27 -000000000 00:04:59.024974 -4

fiddle

3
  • I tried in different way below is my answer. Thank you for your answers. Help me in correcting my below solution if it is wrong. Commented Jul 7 at 12:40
  • Seems like the query is not able to fetch records for me. Can you check question again I have updated few more details about my system NLS in notes . Is that the issue causing not able to get any reocrds. Commented Jul 7 at 13:16
  • could you help me to resolve it Commented Jul 7 at 13:43
1

I tried it in this way and it worked. In case any issue in mu query. Kindly help me with correcting it along with explanation.

SELECT
    id,
    modified_date,
    EXTRACT(MINUTE FROM CAST(current_timestamp AS TIMESTAMP))                                                         AS cur_minutes,
    EXTRACT(MINUTE FROM CAST(modified_date AS TIMESTAMP))                                                             AS mod_minutes,
    EXTRACT(MINUTE FROM CAST(current_timestamp AS TIMESTAMP)) - EXTRACT(MINUTE FROM CAST(modified_date AS TIMESTAMP)) AS min_diff
FROM
    demo
WHERE
    modified_date > current_timestamp - INTERVAL '5' MINUTE;
1
  • This is wrong as if the current time is 12:03:00 and the modified time is 11:59:00 then 3 - 59 = -56 when the actual difference is 4 minutes. You need to extract the minutes from the interval difference (after subtracting) and not from the from the times (before subtracting). I have updated my answer.
    – MT0
    Commented Jul 7 at 12:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.