Here is one of our query in the database, i am trying to understand some UTC converstion in our query.
Can some one briefly explain, what the below query is doing?
SELECT
CAST (SYS_EXTRACT_UTC (CAST ( (BEGIN_DATE - (3 / 24)) AS TIMESTAMP)) AS DATE) BEGIN_DATE
FROM offer o
WHERE mask = 'OK'
AFTER CONVERSION OUTPUT : 06-SEP-11 04:00:00
SELECT
BEGIN_DATE
FROM offer o
WHERE mask = 'OK'
BEFORE CONVERSION OUTPUT : 06-SEP-11 00:00:00
SYS_EXTRACT_UTC extracts the UTC (Coordinated Universal Time—formerly Greenwich Mean Time) from a datetime value with time zone offset or time zone region name.
For example,
SQL> SELECT SYS_EXTRACT_UTC(TIMESTAMP '2014-11-05 12:00:00.00 -08:00') as dt
2 FROM DUAL;
DT
---------------------------------------------------------------------------
05-NOV-14 08.00.00.000000000 PM
SQL>
In your case, your timezone is -08:00. So, -3/24 is used.
For example, my timezone is +05:30. The current time is,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh:mi:ss am') dt from dual;
DT
----------------------
11/04/2014 01:12:21 pm
SQL>
The UTC time equivalent is,
SQL> SELECT SYS_EXTRACT_UTC(TIMESTAMP '2014-11-05 01:12:21.00 +05:30') as dt
2 FROM DUAL;
DT
---------------------------------------------------------------------------
04-NOV-14 07.42.21.000000000 PM
SQL>
Related
I have a column in table A as
select create_time from table_a;
The value is
08-MAR-19 08.23.47.897000000 PM GMT.
This column has been marked as VARCHAR2 for some business purpose. Now I am trying to get this column and convert the value to TIMESTAMP for some purpose, like this as below:
SELECT TO_TIMESTAMP(create_time, 'DD-MON-YYYY HH.MI.SS.FF AM') from table_a;
But I am getting error:
ORA-01830: date format picture ends before converting entire input string
Can someone help me to convert this varchar data to timestamp. The reason I am trying to do is, I need to convert this time from one timezone to another :
eg:
SELECT FROM_TZ(TO_TIMESTAMP(create_time, 'DD-MON-YYYY HH.MI.SS.FF AM'), 'UTC') AT TIME ZONE 'CET' from table_a;
SQL> select replace('08-MAR-19 08.23.47.897000000 PM GMT','GMT','') AS RESULT from dual
;
RESULT
--------------------------------
08-MAR-19 08.23.47.897000000 PM
SQL> select to_timestamp(replace('08-MAR-19 08.23.47.897000000 PM GMT','GMT','')) as RESULT from dual ;
RESULT
---------------------------------------------------------------------------
08-MAR-19 08.23.47.897000000 PM
SQL> select from_tz(to_timestamp(replace('08-MAR-19 08.23.47.897000000 PM GMT','GMT','')),'UTC') AT TIME ZONE 'CET' AS RESULT from dual ;
RESULT
---------------------------------------------------------------------------
08-MAR-19 09.23.47.897000000 PM CET
SQL>
The four characters GMT at the end are not accounted for in your format string
SELECT TO_TIMESTAMP('08-MAR-19 08.23.47.897000000 PM GMT', 'DD-MON-YYYY HH.MI.SS.FF AM') from dual;
The following takes care of that:
SELECT TO_TIMESTAMP(substr(create_time, 1, LENGTH(create_time) -4), 'DD-MON-YYYY HH.MI.SS.FF AM') t from dual;
Try this.
select TO_TIMESTAMP( REPLACE(ts, 'GMT', '')) from test_timestamp;
Remove GMT using REPLACE
WITH A AS (SELECT REPLACE('08-MAR-19 08.23.47.897000000 PM GMT','GMT','') AS D FROM DUAL)
SELECT TO_TIMESTAMP (D, 'DD-MON-YYYY HH.MI.SS.FF AM')
FROM A
Doing something like:
to_timestamp(replace(create_time ,'GMT', null))
relies on your NLS settings, both for the timestamp format - particularly that it has an RR year mask - and the language for the month abbreviation. It would be safer to do:
to_timestamp(replace(create_time, ' GMT', null),
'DD-MON-RR HH.MI.SS.FF AM', 'NLS_DATE_LANGUAGE=ENGLISH')
If the time zone isn't always GMT, but is always a valid and recognised region (not BST, for instance) then you might want to preserve the full date/time including that zone:
to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH')
If you want that as a plain timestamp you can cast it, possibly changing to a specific zone first:
cast(to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH') as timestamp)
or
cast(to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH') at time zone 'Asia/Tokyo' as timestamp)
or normalised to UTC (which won't affect GMT values, of course, as they're essentially the same):
sys_extract_utc(to_timestamp_tz(create_time, 'DD-MON-RR HH.MI.SS.FF AM TZR',
'NLS_DATE_LANGUAGE=ENGLISH'))
db<>fiddle
I have a table which contains the start date, ExpiryDate, I want to write an oracle query which checks if the expiry date is greater than the current system date, Then I want to return that row, else null will be the result of the query.
I wrote something like this,
select Name,Password,StartDate,ExpiryDate from db_name where UserName = 'abc' and status =1 and ExpiryDate >=(SELECT Round((sysdate - to_date('01-JAN-1970','DD-MON-YYYY')) * (86400))*1000 as dt FROM dual);
Here is the table description:
STARTDATE NOT NULL NUMBER(20)
EXPIRYDATE NOT NULL NUMBER(20)
The values:
EXPIRYDATE
----------
1.5880E+12
after performing query like select to_char(startdate),to_char(expirydate) I am getting
TO_CHAR(STARTDATE)
----------------------------------------
TO_CHAR(EXPIRYDATE)
----------------------------------------
1587909960000
1587996480000
But it is working fine for all cases, but if the expiry date is less than( the current time+6hrs) it is giving null, can anyone tell me how to solve this?
Unix epoch time is in the UTC time zone. You can convert the current time to UTC time zone and then subtract the epoch:
SELECT Name,
Password,
StartDate,
ExpiryDate
FROM IM_USER_MANAGEMENT
WHERE UserName = 'abc'
AND status =1
AND ExpiryDate >= ( CAST( SYSTIMESTAMP AT TIME ZONE 'UTC' AS DATE )
- DATE '1970-01-01'
)*24*60*60*1000
Unix epoch time, eh? See if this helps.
Set date format to something recognizable:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Sample data:
SQL> select * From test;
STARTDATE EXPIRYDATE
---------------------- ----------------------
1587909960000 1587996480000
Converted to DATE values:
SQL> select
2 date '1970-01-01' + ( 1 / 24 / 60 / 60 / 1000) * startdate startdt,
3 date '1970-01-01' + ( 1 / 24 / 60 / 60 / 1000) * expirydate expdt
4 from test;
STARTDT EXPDT
------------------- -------------------
26.04.2020 14:06:00 27.04.2020 14:08:00
Or, using it along with sysdate:
SQL> select *
2 from test
3 where sysdate between
4 date '1970-01-01' + ( 1 / 24 / 60 / 60 / 1000) * startdate and
5 date '1970-01-01' + ( 1 / 24 / 60 / 60 / 1000) * expirydate;
STARTDATE EXPIRYDATE
---------------------- ----------------------
1587909960000 1587996480000
As sysdate currently is:
SQL> select sysdate from dual;
SYSDATE
-------------------
27.04.2020 12:45:56
It looks to me like these dates of yours are Javascript style timestamps. That is, it looks like they are times since the UNIX epoch 1970-01-01 00:00:00 UTC measured in milliseconds. Notice they're with reference to UTC, not your local time zone. Is your time zone Asia/Dhaka? That's the one six hours ahead of UTC.
It also looks to me like your timestamps have ten-second precision. The two you showed are divisible by 10 000.
This is the formula for converting Javascript times to Oracle UTC date/time values
SELECT TO_DATE('19700101','yyyymmdd') + (1587909960000/86400000) FROM DUAL;
This yields a SYSDATE - style rendering of your values in UTC time, not local time. It yields 2020-04-26 14:06:00
Because you have a six-hour apparent error, I guess your local time zone is Asia/Dhaka, UTC+6. But it also could possibly be America/Denver, UTC-6.
and your time value, run through that formula, yields 2020-04-26 14:06:00. Which seems like a valid recent date/time.
This is a GUESS! If you're working with other peoples' money or lives in your database, ask the person who programmed it. It's not a DBMS-native way of doing things, so you should double-check.
What's going on in the formula?
In Oracle, adding 1.0 to a SYSDATE - style value adds one calendar day to it. So we start with the Oracle date for the UNIX epoch TO_DATE('19700101','yyyymmdd').
Then we take your millisecond timestamp value and convert it to days, dividing by 86 400 000, Finally we add it to the epoch date.
Here are some suggestions about getting the current time in UTC, so you can compare it to your timestamp data. How to get UTC value for SYSDATE on Oracle
I need to convert timestamp String in UTC TZ format to CST TZ format as shown in here "2019-01-02T11:53:59.269-05:00"
So basically i need the output of this query with SYSTIMESTAMP replaced with time String in UTC TZ Format.
select TO_CHAR(SYSTIMESTAMP ,'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') CREATEDTIME from dual;
I tried lot of stuff but getting errors
select TO_CHAR( to_timestamp('2019-01-02 11:53:59.759', 'YYYY-MM-DD HH24:MI:SS.FF3TZH') ,'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') CREATEDTIME from dual
Error
ORA-01821: date format not recognized
Could you please help me write the correct SQL query.
With a TSTZ, you can select at a different time-zone.
Below are some examples for daylight- and standard-time.
Standand-Time:
SELECT TO_CHAR(TO_TIMESTAMP_TZ('2019-01-02T16:53:59.269 UTC',
'YYYY-MM-DD"T"HH24:MI:SS.FF3 TZR') AT TIME ZONE 'AMERICA/CHICAGO',
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM')
AS CENTRAL_TIME
FROM DUAL;
Result:
CENTRAL_TIME
2019-01-02T10:53:59.269-06:00
1 row selected.
And a daylight-savings example:
SELECT TO_CHAR(TO_TIMESTAMP_TZ('2019-04-02T17:35:52.136 UTC',
'YYYY-MM-DD"T"HH24:MI:SS.FF3 TZR') AT TIME ZONE 'AMERICA/CHICAGO',
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM')
AS CENTRAL_TIME
FROM DUAL;
Result:
CENTRAL_TIME
2019-04-02T12:35:52.136-05:00
1 row selected.
I have an Oracle table called ACQDATA with a field READDATETIME where I store a Unix timestamp in milliseconds as an INTEGER (NUMBER(38)) type.
SQL> select READDATETIME from ACQDATA where ID=1000;
READDATETIME
____________
1.4793E+12
I need to select that value as a ISO-8601 string (YYYY-MM-DDTHH:MM:SS.mmm):
SQL> select READDATETIME from ACQDATA where ID=1000;
READDATETIME
-------------------
1.4793E+12
I´ve tried to convert it using TO_CHAR, but the result is messy:
SQL> select TO_CHAR(TO_DATE('1970-01-01','YYYY-MM-DD') + NUMTODSINTERVAL(READDATETIME, 'SECOND'), 'YYYY-MM-DD HH24:MI:SS') from ACQDATA where ID=1000;
Error at line 1:
ORA-01873: the leading precision of the interval is too small
Help appreciated.
Alex's answer is not fully correct. Unix timestamp is always based on 1970-01-01 00:00:00 UTC
Unless your session runs on UTC time zone the precise solution would be like this:
select
TO_CHAR((TIMESTAMP '1970-01-01 00:00:00 UTC' + readdatetime/1000 * INTERVAL '1' SECOND) AT LOCAL, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')
from ACQDATA where ID=1000;
or
select
TO_CHAR((TIMESTAMP '1970-01-01 00:00:00' AT TIME ZONE 'UTC' + readdatetime/1000 * INTERVAL '1' SECOND) AT LOCAL, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')
from ACQDATA where ID=1000;
or if you prefer functions instead of literals:
select
TO_CHAR((TO_TIMESTAMP_TZ('1970-01-01 00:00:00 UTC', 'YYYY-MM-DD HH24:MI:SS TZR') + numtodsinterval(readdatetime/1000, 'SECOND')) AT LOCAL, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')
from ACQDATA where ID=1000;
Your readdatetime seems to be in milliseconds. Oracle date arithmetic works on the basis of days, so you need to convert that number to the number of days it represents; one day is 86400 seconds, so it's 86400000 milliseconds:
with acqdata (id, readdatetime) as (
select 1000, 1479318995000 from dual
)
select to_char(date '1970-01-01' + (READDATETIME/86400000), 'YYYY-MM-DD"T"HH24:MI:SS')
from ACQDATA where ID=1000;
TO_CHAR(DATE'1970-0
-------------------
2016-11-16T17:56:35
The T is added as a character literal.
SQL Developer defaults to show numbers that large in scientific notation. You can change that default with set numformat, or use to_char() to show the whole value:
select readdatetime, to_char(readdatetime, '9999999999999') as string
from ACQDATA where ID=1000;
READDATETIME STRING
------------ --------------
1.4793E+12 1479318995000
If your value has fractional seconds, so the last three digits are not zeros, you can convert the date to a timestamp and add on the fractional leftovers; this also adds the UTC 'Z' indicator for fun:
with acqdata (id, readdatetime) as (
select 1000, 1479300462063 from dual
)
select to_char(cast(date '1970-01-01' + (readdatetime/86400000) as timestamp)
+ numtodsinterval(remainder(readdatetime, 1000)/1000, 'SECOND'),
'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
from acqdata where id=1000;
TO_CHAR(CAST(DATE'1970-01-01'+
------------------------------
2016-11-16T12:47:42.063Z
Or without the intermediate date value, starting from a timestamp literal:
with acqdata (id, readdatetime) as (
select 1000, 1479300462063 from dual
)
select to_char(timestamp '1970-01-01 00:00:00'
+ numtodsinterval(readdatetime/1000, 'SECOND'),
'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
from acqdata where id=1000;
TO_CHAR(TIMESTAMP'1970-0
------------------------
2016-11-16T12:47:42.063Z
As #Wernfried ponted out, it's better to explicitly show that the epoch time is starting from UTC:
alter session set time_zone='America/New_York';
with acqdata (readdatetime) as (
select 1479300462063 from dual
union all select 1467331200000 from dual
union all select 1467648000000 from dual
)
select readdatetime,
to_char(timestamp '1970-01-01 00:00:00' + numtodsinterval(readdatetime/1000, 'SECOND'),
'YYYY-MM-DD"T"HH24:MI:SS.FF3') as implicit,
to_char(cast(timestamp '1970-01-01 00:00:00' as timestamp with time zone)
+ numtodsinterval(readdatetime/1000, 'SECOND'),
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') as local_offset,
to_char(timestamp '1970-01-01 00:00:00 UTC' + numtodsinterval(readdatetime/1000, 'SECOND'),
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') as utc_offset,
to_char(timestamp '1970-01-01 00:00:00 UTC' + numtodsinterval(readdatetime/1000, 'SECOND'),
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZR') as utc
from acqdata;
READDATETIME IMPLICIT LOCAL_OFFSET UTC_OFFSET UTC
-------------- ----------------------- ----------------------------- ----------------------------- --------------------------
1479300462063 2016-11-16T12:47:42.063 2016-11-16T12:47:42.063-05:00 2016-11-16T12:47:42.063+00:00 2016-11-16T12:47:42.063UTC
1467331200000 2016-07-01T00:00:00.000 2016-07-01T01:00:00.000-04:00 2016-07-01T00:00:00.000+00:00 2016-07-01T00:00:00.000UTC
1467648000000 2016-07-04T16:00:00.000 2016-07-04T17:00:00.000-04:00 2016-07-04T16:00:00.000+00:00 2016-07-04T16:00:00.000UTC
I have a table wchih has 2 columns. The definition is
CREATE TABLE LOGGING_T
(
TSTAMP DATE,
LINE VARCHAR2(300)
)
TABLESPACE OPERATIONS
MONITORING
/
The colulmn TSTAMP has values like 30-NOV-11, 29-NOV-11 ... and so on. Now i am doing this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 1)
The current system date is 01-DEC-11. Ideally, the above statement should return records which has TSTAMP = 30-NOV-11 since i am doing SYSDATE-1 which would be 30-NOV-11. But it isn't fetching those records. Why?
However, if i do this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 2)
Then it fetches records who TSTAMP is 30-NOV-11. Am i doing something wrong in this simple date operation?
A DATE contains time of day as well as the date.
If SYSDATE was 2011-12-01 1:18:00 PM then SYSDATE-1 would be 2011-11-30 1:18:00 PM.
Are the rows you are expecting to find from November 30th before or after the time element?
If you don't care about the time, and only want to filter based on the date, you can use TRUNC():
select *
from LOGGING_T
where TRUNC(TSTAMP) >= TRUNC(SYSDATE - 1);
You'll may or may not want to make sure both sides of your comparison operator are TRUNC()ed because TRUNC() will just force the time element of the date to be midnight.
select to_char(trunc(sysdate), 'YYYY-MM-DD HH:MI:SS PM')
from dual;
NOW
----------------------
2011-12-01 12:00:00 AM
The value SYSDATE has the time component as well. Most probably the date in your database also has the time component.
Change your query to :
select * from LOGGING_T where TSTAMP >= TRUNC(SYSDATE - 1)
to see all records which were logged from 00:00 yesterday.
To see the actual timecomponents, use to char.
SQL> select sysdate from dual;
SYSDATE
---------
01-DEC-11
1* select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS') date1 from dual
SQL> /
DATE1
--------------------
01-Dec-2011 16:29:01