Round Timestamp to nearest Hour in teradata - sql

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')

Related

Convert Timestamp to minutes in Oracle after a subtraction

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

SQL extract hour from timestamp

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

Oracle query on time (and not date)

How can you query on just the time portion of an Orace date field. Ie:
select * from mytable
where
date_column < '05:30:00'
Want the query to return any rows where the time represented by date_column is less than 5:30 regardless of the date.
You can try like this:
select * from mytable
where
to_char( date_column, 'HH24:MI:SS' ) < '05:30:00'
You can see how far the date is from midnight, and filter on that:
select * from mytable
where date_column - trunc(date_column) < 5.5/24
The date_column - trunc(date_column) calculation will give you a fraction of a day, as is normal for date arithmetic. The 5.5/24 is the fraction of the day represented by the time at 05:30; 5.5 hours out of 24 hours.
If the column was a timestamp instead of a date you'd see an interval data type as the result of the subtraction. You can use an interval literal anyway if you prefer or find it easier to understand than 5.5/24 (or have more complicated times to compare, which are harder to express as a fraction):
select * from mytable
where date_column < trunc(date_column) + interval '0 05:30:00' day to second;
This way round you're comparing the date in your column with the truncated date (i.e. midnight on that day) with 5 hours 30 minutes added to it, which is 05:30 the same day.
Quick demo with simple data in a CTE, and a third very slight variant, but they all get the same result:
with mytable (date_column) as (
select to_date('2016-04-15 05:29:29', 'YYYY-MM-DD HH24:MI:SS') from dual
union all select to_date('2016-04-14 05:29:29', 'YYYY-MM-DD HH24:MI:SS') from dual
union all select to_date('2016-04-15 05:30:30', 'YYYY-MM-DD HH24:MI:SS') from dual
)
select * from mytable
where date_column < trunc(date_column) + 5.5/24;
DATE_COLUMN
-------------------
2016-04-15 05:29:29
2016-04-14 05:29:29
Note though that any manipulation of the column like this will prevent an index being used. If you have to do this regularly it might be worth adding a virtual column/index which does that calculation.
You need to cast the time back to date, otherwise you're simply doing string comparison.
Oracle doesn't really have a clever way of doing this. Simplest to cast both dates to the same day and do the comparison.
I.e.
select *
from mytable
where to_date ('01.01.2000 ' || to_char (date_column, 'hh24:mi:ss'), 'dd.mm.yyyy hh24:mi:ss') <
to_date ('01.01.2000' || '05:30:00', 'dd.mm.yyyy hh24:mi:ss')

Condition for current time greater than or equal to from time and current time less than or equal to time in ORACLE

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')

How to I get a current date but with a fixed time in my SQL Where

In my where I want to get is_my_day < current date with a fixed time (ie 0600 hrs). What's the syntax? This is the current format but I want to specify a fixed time for my automated report. I am using Oracle 10.
WHERE
TO_CHAR(MOPACTIVITY.MOPEND, 'yyyy-mm-dd hh24:mi') < TO_CHAR(TRUNC(CURRENT_DATE - 8/24,'hh24'), 'yyyy-mm-dd hh24:mi')
There are several ways to do this:
Option one you compare DATE-values direct
WHERE
MOPACTIVITY.MOPEND <
trunc(SYSDATE, 'DD') + INTERVAL '06:00' HOUR TO MINUTE
SYSDATE - current date , current time
TRUNC(SYSDATE, 'DD') - current date, 00:00 hrs
'TRUNC(SYSDATE, 'DD') + INTERVAL '06:00' HOUR TO MINUTE' - current date, 06:00 hrs
Option two: you convert date values to strings und compare strings
WHERE
TO_CHAR(MOPACTIVITY.MOPEND, 'yyyymmddhh24mi') <
to_char(SYSDATE, 'yyyymmdd') || '0600'
to_char(SYSDATE, 'yyyymmdd') -- returns 20130726
This looks a bit ugly IMO but it's the only thing I can think of for now.
WHERE TO_CHAR(MOPACTIVITY.MOPEND, 'yyyy-mm-dd hh24:mi')
< TO_CHAR(TRUNC(TO_DATE(TRUNC(CURRENT_DATE)) + 6/24, 'hh24'), 'yyyy-mm-dd hh24:mi')