Subtract two date field values and get the minutes - sql

I have below TEMP_PROCESS_MONITOR table which has ALERT_TIMESTAMP column which has date data type.The TIMEOUT column has Number data type and it consist of Minutes values. I want to subtract the Current date with the ALERT_TIMESTAMP field and get the minutes and then compare this minutes with TIMEOUT field in the below select query.
TEMP_PROCESS_MONITOR table:
And the condition should be for example like this:
(CURRENT_TIMESTMAP - ALERT_TIMESTAMP) > TIMEOUT
So i have to remove the Where condition from the below query and put the condition which i have mentioned above:
SELECT COUNT(*) FROM TEMP_PROCESS_MONITOR WHERE IS_DOWN = 1;

To get current date with seconds in date format you may use SYSDATE SQL function.
Date subtraction gives number of days (with fraction) as a result.
So you need either translate this number to minutes:
(SYSDATE - ALERT_TIMESTAMP)*24*60 > TIMEOUT
or translate timeout to days:
(SYSDATE - ALERT_TIMESTAMP) > TIMEOUT/(24*60)

Related

Date Functions Trunc (SysDate)

I am running the below query to get data recorded in the past 24 hours. I need the same data recorded starting midnight (DATE > 12:00 AM) and also data recorded starting beginning of the month. Not sure if using between will work or if there is better option. Any suggestions.
SELECT COUNT(NUM)
FROM TABLE
WHERE
STATUS = 'CNLD'
AND
TRUNC(TO_DATE('1970-01-01','YYYY-MM-DD') + OPEN_DATE/86400) = trunc(sysdate)
Output (Just need Count). OPEN_DATE Data Type is NUMBER. the output below displays count in last 24 hours. I need the count beginning midnight and another count starting beginning of the month.
The query you've shown will get the count of rows where OPEN_DATE is an 'epoch date' number representing time after midnight this morning*. The condition:
TRUNC(TO_DATE('1970-01-01','YYYY-MM-DD') + OPEN_DATE/86400) = trunc(sysdate)
requires every OPEN_DATE value in your table (or at least all those for CNLD rows) to be converted from a number to an actual date, which is going to be doing a lot more work than necessary, and would stop a standard index against that column being used. It could be rewritten as:
OPEN_DATE >= (trunc(sysdate) - date '1970-01-01') * 86400
which converts midnight this morning to its epoch equivalent, once, and compares all the numbers against that value; using an index if there is one and the optimiser thinks it's appropriate.
To get everything since the start of the month you could just change the default behaviour of trunc(), which is to truncate to the 'DD' element, to truncate to the start of the month instead:
OPEN_DATE >= (trunc(sysdate, 'MM') - date '1970-01-01') * 86400
And the the last 24 hours, subtract a day from the current time instead of truncating it:
OPEN_DATE >= ((sysdate - 1) - date '1970-01-01') * 86400
db<>fiddle with some made-up data to get 72 back for today, more for the last 24 hours, and more still for the whole month.
Based on your current query I'm assuming there won't be any future-dated values, so you don't need to worry about an upper bound for any of these.
*Ignoring leap seconds...
It sounds like you have a column that is of data type TIMESTAMP and you only want to select rows where that TIMESTAMP indicates that it is today's date? And as a related problem, you want to find those that are the current month, based on some system values like CURRENT TIMESTAMP and CURRENT DATE? If so, let's call your column TRANSACTION_TIMESTAMP instead of (reserved word) DATE. Your first query could be:
SELECT COUNT(NUM)
FROM TABLE
WHERE
STATUS = 'CLND'
AND
DATE(TRANSACTION_TIMESTAMP)=CURRENT DATE
The second example of finding all for the current month up to today's date could be:
SELECT COUNT(NUM)
FROM TABLE
WHERE
STATUS = 'CLND'
AND
YEAR(DATE(TRANSACTION_TIMESTAMP)=YEAR(CURRENT DATE) AND
MONTH(DATE(TRANSACTION_TIMESTAMP)=MONTH(CURRENT DATE) AND
DAY(DATE(TRANSACTION_TIMESTAMP)<=DAY(CURRENT DATE)

How I filter SQL using curdate? (example: registers that column "created_date" are more than 180 ago) (responsys - oracle)

I need to filter my list based on date time.
Example: "people who purchased 180 days ago"
However I need that the comparison be with actual date (system date), so that I don't have to filter manually every day.
Subtract 180 from (truncated?) SYSDATE:
select whatever
from your_table
where date_column < trunc(sysdate) - 180;
Why truncate? Becuase sysdate function returns both date and time, so - you'd probably want to "remove" the time component.

What is the difference between these 2 sql queries?

select * from accounts where sysdate - 100 < last_active_dt;
select * from accounts where sysdate - 100 <= last_active_dt;
Both of the queries returned the same result.
I was thinking that for the first query, it would not select
records on the day of last_active_dt. For the 2nd query, it
will select records on the day of last_active_dt.
But this does not seem to be the case as both returned the same results.
Is there any difference to it? And how can I see the difference?
last_active_dt column follows a date format DD-MON-YYYY
last_active_dt column follows a date format DD-MON-YYYY
No, it doesn't (but it may look like it does in the user interface you are using).
A DATE data type always has year, month, day, hour, minute and second components and is stored (without any formatting) in 7 bytes. It is the user interface that applies a (default) format to the date and this may hide the time component from you.
You can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
To change the default date format used in SQL*Plus (and SQL Developer) to see the date with all the components.
Equally, SYSDATE is of a DATE data type and has year, month, day, hour, minute and second components so what you are probably intending is:
select * from accounts where TRUNC( sysdate ) - INTERVAL '100' DAY < last_active_dt;
This will get all the columns where the last_active_dt is after midnight of the day 100 days ago (even if it 1 second after). This will work if all your last_active_dt values have the times TRUNCated to exactly midnight 00:00:00 but not if the column as non-zero time components as you will still get values with times from 00:00:01 to 23:59:59 of the day 100 days ago.
If your last_active_dt has a non-zero time component then you want:
select * from accounts where TRUNC( sysdate ) - INTERVAL '99' DAY <= last_active_dt;

Unable to retrieve data for a specific date in SQL

I'm trying to retrieve data for a specific date with query:
Select * from table_1 where activity_date = to_date('13-09-2017','DD-MM-YYYY');
But I'm not getting any result.
But when I query below:
Select * from table_1 where activity_date >= to_date('13-09-2017','DD-MM-YYYY');
I'm getting the data for days greater than equal to 13-09-2017.
Any help?
It seems the data type of your column is Date Time and hence when you are querying with just date (13-09-2017), it searching for date as well as time (13-09-2017 00:00:00). Seems there are no record in your database with this exact date time stamp and hence no results are being returned. Below query will work in your case:
SELECT *
FROM table_1
WHERE activity_date >= to_date('13-09-2017','DD-MM-YYYY')
AND activity_date < to_date('14-09-2017','DD-MM-YYYY');
When you compare dates remember that you usually compare some kind of a timestamp. A timestamp usually also has hours, minutes, seconds etc. So in your database you probably have a whole timestamp like '13-09-2017' + specific hour, minutes and so on, whilst to_date explicitly only formats your DD MM and YYYY causing the hour, minutes and seconds to be equal to 0. The query is probably not returning any records as you probably don't have a date with the exact time equal to 00:00:00.
That's also why a comparison like >= will return results. Because your '13-09-2017' database entries are all greater than '13-09-2017' (with hour equal to 00:00:00) because they all have some hours, minutes and seconds to them, for e.g 13-09-2017 04:02:45 which is greater than 13-09-2017 00:00:00).
How to solve this? Well for example:
SELECT *
FROM table_1
WHERE activity_date >= to_date('13-09-2017','DD-MM-YYYY')
AND activity_date < to_date('14-09-2017','DD-MM-YYYY')

time stamp field to output records from last 24 hours

I need to make an Access query output records that were only from last 24 hours. The field called " SYSADM_CUSTOMER_ORDER.CREATE_DATE" is the time-stamp field. I cant use the criteria ">date()-1", because that would give me records from after 12AM the previous day and I need to run the query at 4PM every day and only output records from after 4PM the previous day. Please give me the preoper SQL for me to copy and paste, based on my SQL below. thank you very much, Nathaniel
SELECT , SYSADM_CUSTOMER_ORDER.ID
FROM SYSADM_CUSTOMER_ORDER;
I think you should probably be using now() - 1, something like:
select * from sysadm_customer_order where create_date > now() - 1;
The date function returns the date with an implicit time of 00:00:00. You want now() which gives you both current date and time.