Casting a specific hour and minute and getting the time difference - sql

I have the following:
trunc(sysdate - to_timestamp(trunc(sysdate, 'YYYY-MM-DD') || ' 12:30', 'YYYY-MM-DD HH245:MI') * 1440,1)
What I'm trying to do is, using the date portion of sysdate and casting 12:30 to the time part. Then calculating the difference between sysdate#12:30 and the current sysdate.
I'm getting:
ORA-00932: inconsistent datatypes: expected NUMBER got TIMESTAMP
after the minus sign.
Any ideas?

You're getting that error because you're trying to multiply a timestamp by 1440, rather than the difference between the date and timestamp - multiplication has higher precedence than subtraction. You also have HH245 instead of HH24 in your format model; trunc(sysdate, 'YYYY-MM-DD') will throw "ORA-01898: too many precision specifiers"; and if you changed that to to_char(sysdate, 'YYYY-MM-DD') || ... you would be subtracting a timestamp from a date which gives you an interval, not a number, and would throw a different ORA-00932 error.
Fixing all of those issues, you would end up with:
trunc((sysdate - to_date(to_char(sysdate, 'YYYY-MM-DD') || ' 12:30', 'YYYY-MM-DD HH24:MI')) * 1440, 1)
which would evaluate to 354.2 for me at the moment.
You don't need to convert to or from string though. You can get 12:30 today with:
trunc(sysdate) + (12.5/24)
where trunc(sysdate) gives you midnight today, and 12.5/24 gives you the fraction of a day for 12.5 hours. You can then subtract that from the current time:
sysdate - (trunc(sysdate) + (12.5/24))
to get the difference in days, then multiple that result by 1440 for the number of minutes, and truncate to a single decimal place.
Quick demo with current date and 12:30 today plus the difference three ways:
select sysdate,
trunc(sysdate) + (12.5/24) as "12:30",
sysdate - (trunc(sysdate) + (12.5/24)) as diff_days,
(sysdate - (trunc(sysdate) + (12.5/24))) * 1440 as diff_mins,
trunc((sysdate - (trunc(sysdate) + (12.5/24))) * 1440, 1) as result
from dual;
SYSDATE 12:30 DIFF_DAYS DIFF_MINS RESULT
------------------- ------------------- ---------- ---------- ----------
2019-08-14 18:24:16 2019-08-14 12:30:00 .246018519 354.266667 354.2
As #GordonLinoff hinted, the today-at-12:30 part could be achieved with an interval instead of a fractional day, e.g.:
trunc(sysdate) + 12.5 * interval '1' hour
or more simply and explicitly:
trunc(sysdate) + interval '12:30' hour to minute
You seem to want your final result as the number of minutes, but you could also get it as an interval, either by converting the current number-of-days intermediate result (or number of minutes) with numtodsinterval():
numtodsinterval(sysdate - (trunc(sysdate) + interval '12:30' hour to minute), 'DAY')
which is a bit convoluted, or directly:
select systimestamp,
systimestamp - trunc(sysdate) - interval '12:30' hour to minute as result
from dual;
SYSTIMESTAMP RESULT
------------------------------------ -------------------
2019-08-14 18:24:16.830116000 +01:00 +00 05:54:16.830116
db<>fiddle

Related

Get systime at a specific time and date - Oracle SQL

Is it possible to do something like this?
SELECT
TO_CHAR(SYSDATE-1, 'MM-DD-YYYY 08:00:00') "Yesterday",
I'd like to get the sysdate from yesterday at 8am
You would write this as:
trunc(sysdate - 1) + interval '8' hour
Or:
trunc(sysdate) - interval '16' hour
Or you can do date arithmetics with integer values rather than intervals:
trunc(sysdate) - 16/24
Truncate the sysdate. That will remove the time portion of the date.
Then add 8/24 (8 hours of the 24), 8/24 equals to 1/3 - 8 hours is a third of a day.
Then display this date value in the format DD-MON-YYYY HH24:MI:SS.
This all adds up to:
SELECT TO_CHAR(TRUNC(SYSDATE) - 1 + 1/3,'DD-MON-YYYY HH24:MI:SS') FROM DUAL;

How to truncate timestamp to minutes?

I have to subtract 5 minutes from current timestamp and floor(truncate) it to nearest minute. Like '2016-02-23 06:10:39.0' should be '2016-02-23 06:05:00.0'.
I have found way to subtract 5 minutes as
systimestamp - interval '5' minute
EDIT 1:
I need timestamp in particular format,
TO_TIMESTAMP((systimestamp - interval '15' minute),'YYYY-MM-DD HH24:MI:SS.ff')
But this is giving
ORA-01830: date format picture ends before converting entire input string
But I am not able to floor it to nearest minute.
Please help. Thanks
You could use TRUNC() with the precision you want. To trunc only till minutes, use MI.
For example,
SQL> SELECT SYSTIMESTAMP, trunc(SYSTIMESTAMP - INTERVAL '5' MINUTE, 'MI') new_tmstmp
2 FROM dual;
SYSTIMESTAMP NEW_TMSTMP
----------------------------------- -------------------
16-MAR-16 04.44.02.379000 PM +05:30 03/16/2016 16:39:00
SQL>
Remember, the above output will be a DATE and not TIMESTAMP. You can explicitly CAST the date as timestamp:
SQL> SELECT SYSTIMESTAMP,CAST(trunc(SYSTIMESTAMP - INTERVAL '5' MINUTE,'MI') AS TIMESTAMP) tm
2 FROM dual;
SYSTIMESTAMP TM
----------------------------------- ------------------------------
16-MAR-16 04.53.25.802000 PM +05:30 2016-03-16 04:48:00.000000
SQL>
You can use trunc():
trunc(systimestamp - interval '5' minute, 'minute')

Last date with time of the month

Need your help to conclude the query to fetch last date time of the sysdate month.
select to_char(last_day(sysdate),'DD-Mon-YYYY HH24:MI:SS') from dual
it gives last date as expected, but I need time as 23:59:00 which is not possible thru above query.
You could use TRUNC on next day i.e. SYSDATE + 1, and then subtract 60 seconds i.e. 60/86400 to get the desired output.
SQL> SELECT to_char((trunc(last_day(sysdate)) +1) - 60/86400,'DD-Mon-YYYY HH24:MI:SS') dt
2 FROM dual;
DT
--------------------
29-Feb-2016 23:59:00
SQL>
You could also use interval '1' minute or interval '60' second instead of 60/86400.
If you just want it for display for some reason you can hard-code the time into the format mask:
select to_char(last_day(sysdate), 'DD-Mon-YYYY "23:59:00"') from dual;
But you probably really want it as a date object, in which case you can add 23 hours and 59 minutes to the truncated (midnight) date, wchi is 1439 of the 1440 minutes in a day:
select to_char(trunc(last_day(sysdate)) + 1439/1440, 'DD-Mon-YYYY HH24:MI:SS')
from dual;
Or you can go to the next day and remove a minute, either with fractional days or with intervals:
select to_char(trunc(last_day(sysdate)) + interval '1' day - interval '1' minute,
'DD-Mon-YYYY HH24:MI:SS') from dual;
Generally if you're working with time periods you want to include up to 23:59:59, which you can also do with any of those methods, but as Damien_The_Unbeliever said in a comment, it's easier to compare against the start of the next period (e.g. < add_months(trunc(sysdate, 'MM'), 1). It's easy to accidentally miss part of a day by not taking the time into account properly, particularly if you actually have a timestamp rather than a date.

SYSDATE but specify the time

I want to say the follow but substitute the date with SYSDATE but the time between is what I want to adjust. What would the syntax be?
where mydatefield between SYSDATE+'0001' and SYSDATE+'2359'
...
WHERE TO_CHAR( MOPACTIVITY.MOPNOTIFICATIONSENDAT , 'yyyy-mm-dd hh24:mi' )
BETWEEN '2013-07-26 00:00:01' AND '2013-07-26 23:59:59'
;
SYSDATE (or any other date column) in Oracle has the time component. So you need to strip that off and then add the hours/minutes/time condition.
Eg. to say current day 10:00 AM to 3:00 PM, you can say
date_column between (trunc(sysdate) + 10/24) and (trunc(sysdate) + 15/24)
Oracle date arithmetic works on the day level. so, +1 will give you the next day, 1/24 will give you an hour and 10/24 will give you 10:00 AM in the current day.
SQL> alter session set nls_date_format = 'DD-Mon-YYYY HH:MI:SS AM';
Session altered.
1 select sysdate,
2 trunc(sysdate),
3 trunc(sysdate) + 10/24,
4 trunc(sysdate) + 15/24
5* from dual
SQL> /
SYSDATE 26-Jul-2013 06:26:07 PM
TRUNC(SYSDATE) 26-Jul-2013 12:00:00 AM
TRUNC(SYSDATE)+10/24 26-Jul-2013 10:00:00 AM
TRUNC(SYSDATE)+15/24 26-Jul-2013 03:00:00 PM
For your question, you seem to be interested between current day and next day, so you can try adding + 1 to the date directly, once you strip the time component.
date_column >= trunc(sysdate) and
date_column < trunc(sysdate)+1
The best way to do this is to leave your MOPACTIVITY.MOPNOTIFICATIONSENDAT as a DATE type. That allows Oracle to optimize the query if there happens to be an index on the column. I'd recommend something like this:
WHERE MOPACTIVITY.MOPNOTIFICATIONSENDAT >= TRUNC(SYSDATE)
AND MOPACTIVITY.MOPNOTIFICATIONSENDAT < TRUNC(SYSDATE) + 1
That boils down to "greater than or equal to today at midnight" and "less than tomorrow at midnight".
We can also trunc both the dates and then compare the result
where TRUNC(MOPACTIVITY.MOPNOTIFICATIONSENDAT) = TRUNC(SYSDATE)
TRUNC Removes the timestamp from the dates

Convert milliseconds to Timestamp

I know that to convert a Unix timestamp in milliseconds to an SQL timestamp I can use
SELECT TO_DATE('1970-01-01','YYYY-MM-DD HH24:MI:SS') +
(:timestamp / (1000*60*60*24)) FROM DUAL;
But I need a Timestamp, so I tried with
SELECT TO_TIMESTAMP('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SSFF3') +
(:timestamp) from DUAL
Which gives me the error:
Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0
It seems that adding 1 to the timestamp always converts it to a day.
How can I do the same to get a real timestamp?
You will get a timestamp if you add an interval to a timestamp (see date/interval arithmetics).
As Benoit noticed, you can't specify an interval with seconds when there are more than about 2.1e9 of them:
SQL> SELECT numtodsinterval(2.2e9, 'SECOND'),
2 numtodsinterval(2.3e9, 'SECOND')
3 FROM dual;
NUMTODSINTERVAL(2.2E9,'SECOND' NUMTODSINTERVAL(2.3E9,'SECOND'
------------------------------- -------------------------------
+000024855 03:14:07.147483647 +000024855 03:14:07.147483647
This is why you should use minutes which do not lose precision. For example, assuming :TS is the unix timestamp (i.e. a number):
SQL> variable ts number;
SQL> -- determining unix timestamp with nanosecond precision
SQL> BEGIN
2 :ts := (to_date('2099-01-01 01:02:03', 'yyyy-mm-dd hh24:mi:ss')
3 - date '1970-01-01') * 1000*60*60*24
4 + 123.456789;
5 END;
6 /
ts
---------
4070912523123,456789
SQL> select timestamp '1970-01-01 00:00:00'
2 + numtodsinterval((:ts)/1000/60, 'MINUTE')
3 from dual;
TIMESTAMP'1970-01-0100:00:00'+NUMTODSINTERVAL((:TS)/1000/60,'MINUTE')
---------------------------------------------------------------------------
2099-01-01 01:02:03.123456789
There are two types:
Timestamps
Intervals
Intervals is what you get when you subtract timestamps, and it is nonsensical to add timestamps together.
If you need to get a millisecond interval, I would suggest to use a second interval and divide it by 1000:
I could suggest:
SELECT timestamp'1970-01-01 00:00:00' + (interval '1888' second(9) / 1000)
FROM dual
The problem here is that you cannot use more than 9 digits in a same timestamp literal.
If you need to ad 2,061,464,797,255 milliseconds to the epoch I can suggest:
SELECT TIMESTAMP'1970-01-01 00:00:00'
+ INTERVAL '2' SECOND(9) * 1000000000
+ INTERVAL '061464797' SECOND(9)
+ INTERVAL '255' SECOND(3) / 1000
FROM dual
You get 2035-04-29 13:06:37.255000000
It seems to be subject to the 2038 bug: TIMESTAMP'1970-01-01 00:00:00' + 3 billion seconds does not work, whereas it works with 2 billion.
I've posted here some methods to convert nanoseconds to timestamp and timestamp to nanoseconds. These methods are not affected by time zones and have a nanosecond precision.
You just need to adjust it to use milliseconds instead of nanoseconds.
SELECT TIMESTAMP '1970-01-01 00:00:00 UTC' + numtodsinterval(
1598434427263 --Replace line with desired milliseconds
/ 1000, 'SECOND') AS TIMESTAMP FROM dual;
TIMESTAMP
26/08/20 09:33:47,263000000 UTC
Use
SELECT TIMESTAMP '1970-01-01 00:00:00.1234' + INTERVAL '1 00:00:00' DAY TO SECOND
AS ts
FROM dual;