Subtracting hours from Unix Timestamp - sql

The following query selects a unix timestamp. It should be 1pm but says 5pm because of UTC. It needs to be Eastern time 1pm, so I need to subtract 4 hours from it. What's the best way to go about this?
SELECT CAST(to_date('1970-01-01', 'YYYY-MM-DD') + substr(STARTTIME,0,10)/60/60/24 as timestamp)

You shouldn't caluclate manually. You should add or substract the timezone.
Oracle how to convert time in UTC to the local time (offset information is missing)
Here an example-query:
SELECT TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'YYYY-MM-DD HH24:MI:SS TZH:TZM TZR')
AS BERLIN_Time_complete,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'UTC',
'YYYY-MM-DD HH24:MI:SS TZH:TZM TZR')
AS UTC_Complete,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'YYYY-MM-DD HH24:MI:SS TZH:TZM')
AS BERLIN_Time_complete,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'TZH:TZM')
AS BERLIN_Timezone,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'TZR')
AS Timezone_Name
FROM DUAL;
The key is for Format:
YYYY: Year with four digits (0000-9999 ex: 2018)
MM: Month with two digits (01-12)
DD: Day with two digits (01-31)
HH24: Hour 00-23
MI: Minutes 00-59
SS: Seconds 00-59
TZH: Timezone-Hours
TZM: Timezone-Minutes (There are timezones with 30mins offset)
TZR: Name of the timezone
You should play around with those, to understand the to_date()/to_char(). You'll need it.
If you realy want to add hours. Here an example:
select sysdate + INTERVAL '2' HOUR from dual;

You can try this :
SELECT TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS') AS myDate
FROM dual;
For more information read this document.

Related

Convert epoch time to timestamp in oracle database with system timezone

Can someone please suggest me how to convert epoch time to timestamp in oracle including the database timezone. I am currently using below code:
cast ( TO_TIMESTAMP_TZ('1970-01-01 00:00:00.0 UTC', 'YYYY-MM-DD HH24:MI:SS.FF TZR') + NUMTODSINTERVAL(value/1000, 'SECOND') as timestamp with local time zone)
But it appears this is converting to the sessiontimezone. I need it to be converted to dbtimezone
(In my case select dbtimezone gives PST time and select sessiontimezone gives Asia/Calcutta time.)
Data type timestamp with local time zone always shows date/time at SESSIONTIMEZONE
Try
(TO_TIMESTAMP_TZ('1970-01-01 00:00:00.0 UTC', 'YYYY-MM-DD HH24:MI:SS.FF TZR') + NUMTODSINTERVAL(value/1000, 'SECOND')) AT TIME ZONE DBTIMEZONE
Or with literals:
(Timestamp '1970-01-01 00:00:00 UTC' + value/1000 * INTERVAL '1' SECOND) AT TIME ZONE DBTIMEZONE

CAST(CURRENT_TIMESTAMP AS TIME)

i am trying to work with timestamps and manipulate the data out of them so i can measure different things in our database.
currently i have a column that holds a "DATE" but in fact contains a whole stamp 'DD/MM/YYYY HH24:MI:SS'
i want to be able to grab all entries where "DTIME" between "09:00" and "09:15" however have not been able to cast it correctly.
If i was to output the column without any conversion it would look like this
SELECT ia.DTIME3
FROM ISIS_AUDIT ia
WHERE ia.DTIME3 like to_date('24/01/2019', 'DD/MM/YYYY');
OUTPUT: 24/JAN/19
if i was to convert it to_char,
SELECT to_char(ia.DTIME3, 'DD/MM/YYYY HH24:MI:SS')
FROM ISIS_AUDIT ia
WHERE ia.DTIME3 like to_date('24/01/2019', 'DD/MM/YYYY');
OUTPUT: 24/01/2019 07:10:52
I want to be able to take this DTIME3 and find entries between the times but CAST and CONVERT to TIME doesnt work.
This is my working option but it outputs the date still and i dont want to have to specify the date so it can be run across any day of the week.
WHERE ia.DTIME3 between to_date('24/01/2019 09:00:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('24/01/2019 09:15:00', 'DD/MM/YYYY HH24:MI:SS');
OUTPUT: 24/01/2019 09:00:01
currently i have a column that holds a "DATE" but in fact contains a whole stamp 'DD/MM/YYYY HH24:MI:SS'
That's what Oracle DATE datatype does : storing a date and time (without fractional seconds, that belong to the TIMESTAMP datatypes). There is no specific format in Oracle for date only (without time).
To filter on the time of the day, you can use the TO_CHAR() function to convert your date to a string that represents its time, and that you can compare :
TO_CHAR(ia.DTIME3, 'hh24:mi') BETWEEN '09:00' AND '09:14'
You can also CAST the date to a timestamp and use the EXTRACT() function :
EXTRACT(HOUR FROM CAST(ia.DTIME3 AS TIMESTAMP)) = 9
AND EXTRACT(MINUTE FROM CAST(ia.DTIME3 AS TIMESTAMP)) < 15
Oracle has DATE and TIMESTAMP data types; both have year, month, day, hour, minute, second components (TIMESTAMP also has fractional seconds). Oracle does not have a TIME data type.
Instead, use TRUNC() to truncate the time component to midnight and add an interval literal:
SELECT *
FROM ISIS_AUDIT
WHERE DTIME BETWEEN TRUNC( DTIME ) + INTERVAL '09:00' HOUR TO MINUTE
AND TRUNC( DTIME ) + INTERVAL '09:15' HOUR TO MINUTE;
and ia.DTIME1 not between to_date('&&S_DATE 08:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 09:20:00', 'DD/MM/YYYY HH24:MI:SS')
and ia.DTIME1 not between to_date('&&S_DATE 11:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 12:40:00', 'DD/MM/YYYY HH24:MI:SS')
and ia.DTIME3 not between to_date('&&S_DATE 08:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 09:20:00', 'DD/MM/YYYY HH24:MI:SS')
and ia.DTIME3 not between to_date('&&S_DATE 11:50:00', 'DD/MM/YYYY HH24:MI:SS') and to_date('&&S_DATE 12:40:00', 'DD/MM/YYYY HH24:MI:SS')
i was trying to do this way however i noticed if i have a start time of 08:30 and a finish time of 09:30 then it falls between 09:00 and 09:15 so i want to exclude it from the table. the current WHERE clause im using is quite specific and will only exclude if the timestamp contains the values specified.
Really it would want to read "DTIME1 to DTIME3 not between 09:00 and 09:15"

sqlplus set explicit time

I am using the following query to extract data. But, I want the time to be exact instead of variable.
WHERE DAILY_OPEN_POSITIONS.REPORT_TIMESTAMP <= SYSTIMESTAMP - INTERVAL '3' HOUR + INTERVAL '1' MINUTE
AND DAILY_OPEN_POSITIONS.REPORT_TIMESTAMP >= SYSTIMESTAMP - INTERVAL '3' HOUR - INTERVAL '1' MINUTE
I want the time to be given like this:
where REPORT_TIMESTAMP BETWEEN '2016-08-11 20:59:00' AND '2016-08-11 20:59:01'
The time is a constant, but the date always points to yesterday, that's why I used the first query. is there anyway to mix both?
Time stamp between yesterday's date, time between 20:59:00 and 20:59:01.
Functions used: to_timestamp , to_char , sysdate
where REPORT_TIMESTAMP BETWEEN
TO_TIMESTAMP (to_char(sysdate-1,'DD-Mon-RR') || ' 20:59:00', 'DD-Mon-RR HH24:MI:SS')
and
TO_TIMESTAMP (to_char(sysdate-1,'DD-Mon-RR') || ' 20:59:01', 'DD-Mon-RR HH24:MI:SS')

Oracle SQL: convert UTC to CST

I would like to convert UTC date/time to local CST.
The below function works however it gives 6 hours difference when there should only be 5 hours (until day light saving on 11/2/2014).
CAST((FROM_TZ(CAST(utc_date AS TIMESTAMP),'UTC') AT TIME ZONE 'CST') AS DATE) cst_date
also tried a variation
to_date(to_char((from_tz(to_timestamp(to_char(utc_date, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') ,'UTC')
at time zone 'CST'),'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') as cst_date,
Using the "US/Central" as the target timezone seems to produce the right result.
select from_tz(CAST ('15-oct-2014' AS TIMESTAMP),'GMT') at TIME ZONE 'US/Central' with_daylight_savings,
from_tz(CAST ('15-nov-2014' AS TIMESTAMP),'GMT') at TIME ZONE 'US/Central' without_daylight_savings
from dual;
WITH_DAYLIGHT_SAVINGS WITHOUT_DAYLIGHT_SAVINGS
--------------------------------------------------------------------------------------
14-OCT-14 07.00.00.000000000 PM US/CENTRAL 14-NOV-14 06.00.00.000000000 PM US/CENTRAL
Use timezone region instead of timezone abbr ('CST'). You may find the desired timezone here:
SELECT * from v$timezone_names where tzabbrev = 'CST';
Maybe you need 'CST6CDT' instead of 'CST'
Maybe a stupid approach, but what do you get from this query?
SELECT
TO_CHAR((TIMESTAMP '2014-01-01 00:00:00' + LEVEL * INTERVAL '1' DAY) AT TIME ZONE 'America/Chicago', 'yyyy-mm-dd hh24:mi TZH:TZM') AS dst,
TO_CHAR((FROM_TZ(CAST(DATE '2014-01-01' AS TIMESTAMP), 'UTC') + LEVEL * INTERVAL '1' DAY) AT TIME ZONE 'America/Chicago', 'yyyy-mm-dd hh24:mi TZH:TZM') AS dst
FROM dual
CONNECT BY LEVEL <= 365;
Is it as expected?

ORACLE SQL: add time to a timestamp with timezone

I need to add seconds and substract variables with type TIMESTAMP WITH TIMEZONE, however, to my understanding, adding numbers to such data causes the information about the timezone to be lost, perhaps because it's converted to a DATE type
That is:
SELECT FROM_TZ(
TO_TIMESTAMP(
TO_DATE('03/09/2012 2:30:30','DD/MM/YYYY HH:MI:SS')
)
, 'America/Chicago')
FROM DUAL;
Gives:
03/09/2012 00:00:00, -05:00
Then
SELECT FROM_TZ(
TO_TIMESTAMP(
TO_DATE('03/09/2012 2:30:30','DD/MM/YYYY HH:MI:SS')
)
, 'America/Chicago') + 1/24 -- add 1 hour
FROM DUAL;
Gives
03/09/2012 01:00:00
and loses the timezone information.
But
SELECT FROM_TZ(
TO_TIMESTAMP(
TO_DATE('03/09/2012 2:30:30','DD/MM/YYYY HH:MI:SS'))
, 'America/Chicago') + INTERVAL '1' hour
FROM DUAL;
Correctly gives
03/09/2012 01:00:00,000000000 -05:00
However the INTERVAL.. syntax expect a char constant, so I can't use that with variables.
How can I perform that kind of arithmetic with TIMESTAMP WITH TIME ZONE datatype while retaining timezone information?
TIA
You can use the NUMTODSINTERVAL function to convert a number to an interval.
SELECT FROM_TZ( TIMESTAMP '2012-10-08 00:00:00','-5:00')
+ NUMTODSINTERVAL(1,'HOUR')
FROM dual;