I'm trying to get the minute value from a Timestamp after a subtraction
First I made a subtraction:
Current Time - Target Time(INSP_FIN_DT)
(
TO_TIMESTAMP(TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') -
TO_TIMESTAMP(INSP_FIN_DT,'YYYY-MM-DD HH24:MI:SS')
) AS REST_TIME
the output:
+00 00:09:44.000000
What I need:
09
I already tried
SELECT SUBSTR(REST_TIME, 7,2)
But the return is ever 00, even If I convert it to Char like:
SELECT SUBSTR(TO_CHAR(REST_TIME),7,2)
How can I solve it by query?
Information here https://dba.stackexchange.com/questions/53924/how-do-i-get-the-difference-in-minutes-from-2-timestamp-columns
select
round(
(SYSDATE - cast(<other_timestamp> as date))
* 24 * 60
) as diff_minutes
from <some_table>;
For my current implementation, I'll never have more than a hour, in this case I used the bellow solution:
SYSTIMESTAMP - TO_TIMESTAMP(INSP_FIN_DT,'YYYY-MM-DD HH24:MI:SS') AS REST_TIME
Select..
EXTRACT(MINUTE FROM REST_TIME) AS REST_MINUTES
In case of more than 60 minutes, I should use:
(EXTRACT(HOUR FROM REST_TIME) * 60)
+ EXTRACT(MINUTE FROM REST_TIME) AS REST_MINUTES
Related
How do I convert a Timestamp to the hour in teradata?
For ex
'2020-12-07 11:25:00.000000' to '2020-12-07 11:00:00.000000'
'2020-12-07 11:45:00.000000' to '2020-12-07 11:00:00.000000'
Gordon's solution might not work and return a DATE instead of a TIMESTAMP, depending on a global dbscontrol setting.
Then the most efficient solution is
ts - extract(minute from ts)*interval '1' minute
- extract(second from ts)*interval '1' second
Needs more resources:
to_timestamp(to_char(ts, 'yyyy-mm-dd hh'), 'yyyy-mm-dd hh')
You can use trunc():
select trunc(col, 'HH24')
If you want to round then you can add 30 minutes to your timestamp and then use trunc as follows:
select trunc(ts_col + interval '30' minute, 'HH')
I have come across a query which has me curious whether the programmer was show boating or whether there is merit to the way it has been done in terms of performance. I have no clue as to why the from time is 01:59 rather than 00:00, this would actually remove some of the results that would actually want to be included.
This is the where clause of the query
WHERE REPORTDATE BETWEEN TRUNC(SYSDATE - 21) + 01 / 24 + 59 / (24 * 60) + 59 / (24 * 60 * 60)
AND TRUNC(SYSDATE) + 23 / 24 + 59 / (24 * 60) + 59 / (24 * 60 * 60)
and if my math is correct, is the same as
WHERE REPORTDATE BETWEEN to_date('13/04/2017 01:59','dd/mm/yyyy hh24:mi')
AND to_date('04/05/2017 23:59','dd/mm/yyyy hh24:mi')
Is there any benefit in the first calculated where clause over the second?
You can use interval literals to get rid of all the arithmetic and simplify the query:
WHERE REPORTDATE BETWEEN TRUNC( SYSDATE ) - INTERVAL '20 22:00:01' DAY TO SECOND
AND TRUNC( SYSDATE ) + INTERVAL '00 23:59:59' DAY TO SECOND
or
WHERE REPORTDATE BETWEEN TRUNC( SYSDATE ) - INTERVAL '21' DAY
+ INTERVAL '01:59:59' HOUR TO SECOND
AND TRUNC( SYSDATE ) + INTERVAL '00 23:59:59' DAY TO SECOND
or
WHERE REPORTDATE >= TRUNC( SYSDATE ) - INTERVAL '20 22:00:01' DAY TO SECOND
AND REPORTDATE < TRUNC( SYSDATE ) + INTERVAL '1' DAY
It is hard to imagine a performance difference, based on different ways of calculating constants in a query.
I would write this using something like this:
WHERE REPORTDATE >= CAST(TIMESTAMP '2017-04-13 02:00:00' as DATE) and
REPORTDATE < DATE '2017-05-05'
If you are going to include date/time constants, use the built-in mechanisms that support standard formats.
or for more flexibility based on the current date:
WHERE REPORTDATE >= TRUNC(sysdate) - 21 + 2 / 24 AND
REPORTDATE < TRUNC(sysdate) + 1
(or, if 1:59 is really intended . . . then TRUNC(sysdate) - 21 + (1 * 60 + 59) / (24 * 60).)
This query: select EXTRACT(HOUR FROM 1435047532) as hour from TABLENAME;
Returns this error: "invalid extract field for extract source".
I'm trying to extract the hour from a given timestamp. Maybe the problem is the format of timestamp field is NUMBER and not TIMESTAMP?
You can convert your numeric seconds-since-epoch time to a TIMESTAMP type using:
TIMESTAMP '1970-01-01 00:00:00' + NUMTODSINTERVAL( your_time_since_epoch, 'SECOND' )
So to get the hours:
SELECT EXTRACT( HOUR FROM TIMESTAMP '1970-01-01 00:00:00'
+ NUMTODSINTERVAL( 1435047532, 'SECOND' ) )
FROM DUAL;
If you need to handle leap seconds then you will need to create a function/package to handle this.
try this
select extract(hour from (select to_date('19700101', 'YYYYMMDD')
+ ( 1 / 24 / 60 / 60 ) * 1435047532 from dual)) from dual
Condition not working because using TO_CHAR(),but how to extract time? following is my query:
SELECT *
FROM SUPPLY_TIMING
WHERE TO_CHAR (SYSDATE, 'HH:Mi') >= TO_CHAR (FROM_TIME, 'HH:Mi')
AND TO_CHAR (SYSDATE, 'HH:Mi') <= TO_CHAR (TO_TIME, 'HH:Mi')
It looks like you are interested only in the time but not in the date part. Assuming data type of FROM_TIME and TO_TIME are DATE or TIMESTAMP you can do this:
select *
FROM SUPPLY_TIMING
WHERE EXTRACT(HOUR FROM SYSDATE) + EXTRACT(MINUTE FROM SYSDATE)/60
BETWEEN EXTRACT(HOUR FROM FROM_TIME) + EXTRACT(MINUTE FROM FROM_TIME)/60
AND EXTRACT(HOUR FROM TO_TIME) + EXTRACT(MINUTE FROM TO_TIME)/60
At first glance, my guess is that your format mask is what's causing the problem. The HH component will give you the hours in twelve hour format, so 1pm will be returned as 01, I think if you used HH24 instead, you would get 13, making the string comparison work as intended:
SELECT *
FROM SUPPLY_TIMING
WHERE TO_CHAR (SYSDATE, 'HH24:Mi') >= TO_CHAR (FROM_TIME, 'HH24:Mi')
AND TO_CHAR (SYSDATE, 'HH24:Mi') <= TO_CHAR (TO_TIME, 'HH24:Mi')
I am just trying to add 1 hour to a value, it is kind of complicated on where and why i am doing this but basically i just need to query something like this
select DATE_ADD(hh,1,'2014-10-15 03:30:00 pm') from dual
I keep reading old articles that say to use dateAdd or date_add but I keep getting invalid identifier errors.
select sysdate + 1/24 from dual;
sysdate is a function without arguments which returns DATE type
+ 1/24 adds 1 hour to a date
select to_char(to_date('2014-10-15 03:30:00 pm', 'YYYY-MM-DD HH:MI:SS pm') + 1/24, 'YYYY-MM-DD HH:MI:SS pm') from dual;
Use an interval:
select some_date_column + interval '1' hour
from your_table;
You can use INTERVAL type or just add calculated number value - "1" is equal "1 day".
first way:
select date_column + INTERVAL '0 01:00:00' DAY TO SECOND from dual;
second way:
select date_column + 1/24 from dual;
First way is more convenient when you need to add a complicated value - for example, "1 day 3 hours 25 minutes 49 seconds".
See also: http://www.oracle-base.com/articles/misc/oracle-dates-timestamps-and-intervals.php
Also you have to remember that oracle have two interval types - DAY TO SECOND and YEAR TO MONTH.
As for me, one interval type would be better, but I hope people in oracle knows, what they do ;)
Old way:
SELECT DATE_COLUMN + 1 is adding a day
SELECT DATE_COLUMN + N /24 to add hour(s) - N being number of hours
SELECT DATE_COLUMN + N /1440 to add minute(s) - N being number of minutes
SELECT DATE_COLUMN + N /86400 to add second(s) - N being number of seconds
Using INTERVAL:
SELECT DATE_COLUMN + INTERVAL 'N' HOUR or MINUTE or SECOND - N being a number of hours or minutes or seconds.
To add/subtract from a DATE, you have 2 options :
Method #1 :
The easiest way is to use + and - to add/subtract days, hours, minutes, seconds, etc.. from a DATE, and ADD_MONTHS() function to add/subtract months and years from a DATE. Why ? That's because from days, you can get hours and any smaller unit (1 hour = 1/24 days), (1 minute = 1/1440 days), etc... But you cannot get months and years, as that depends on the month and year themselves, hence ADD_MONTHS() and no add_years(), because from months, you can get years (1 year = 12 months).
Let's try them :
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints current date: 19-OCT-2019 20:42:02
SELECT TO_CHAR((SYSDATE + 1/24), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour: 19-OCT-2019 21:42:02
SELECT TO_CHAR((SYSDATE + 1/1440), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 minute: 19-OCT-2019 20:43:02
SELECT TO_CHAR((SYSDATE + 1/86400), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 second: 19-OCT-2019 20:42:03
-- Same goes for subtraction.
SELECT SYSDATE FROM dual; -- prints current date: 19-OCT-19
SELECT ADD_MONTHS(SYSDATE, 1) FROM dual; -- prints date + 1 month: 19-NOV-19
SELECT ADD_MONTHS(SYSDATE, 12) FROM dual; -- prints date + 1 year: 19-OCT-20
SELECT ADD_MONTHS(SYSDATE, -3) FROM dual; -- prints date - 3 months: 19-JUL-19
Method #2 : Using INTERVALs, you can or subtract an interval (duration) from a date easily. More than that, you can combine to add or subtract multiple units at once (e.g 5 hours and 6 minutes, etc..)
Examples :
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints current date: 19-OCT-2019 21:34:15
SELECT TO_CHAR((SYSDATE + INTERVAL '1' HOUR), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour: 19-OCT-2019 22:34:15
SELECT TO_CHAR((SYSDATE + INTERVAL '1' MINUTE), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 minute: 19-OCT-2019 21:35:15
SELECT TO_CHAR((SYSDATE + INTERVAL '1' SECOND), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 second: 19-OCT-2019 21:34:16
SELECT TO_CHAR((SYSDATE + INTERVAL '01:05:00' HOUR TO SECOND), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour and 5 minutes: 19-OCT-2019 22:39:15
SELECT TO_CHAR((SYSDATE + INTERVAL '3 01' DAY TO HOUR), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 3 days and 1 hour: 22-OCT-2019 22:34:15
SELECT TO_CHAR((SYSDATE - INTERVAL '10-3' YEAR TO MONTH), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date - 10 years and 3 months: 19-JUL-2009 21:34:15
The calculation is simple
if you want to add 1 hour in the date .
every day have 24 hour , you can add .
select sysdate + 1/24 from dual;
if you want 1 day to add
select sysdate + 24/24 from dual;
or
select sysdate + 1 from dual;
same as for 2, 3 , 4 day
For static date you have the answer below.