I would like to know how to convert current time to TIMEZONE in oracle
I tried this and worked perfectly
SELECT (select tzname from qct_timezone_config tzc where gmtoffset =
(select usr_time_zone from qct_user_token where resource_id = 11385)) AS TIMEZONE,
TO_CHAR(CAST(SYSTIMESTAMP AS TIMESTAMP WITH TIME ZONE) AT TIME ZONE
(select tzname from qct_timezone_config tzc where gmtoffset =
(select usr_time_zone from qct_user_token where resource_id = 11385)),
'DD-MON-RR HH:MI:SS.FF AM') AS USER_TIME
FROM dual
But what i want to know is what if the time '03-AUG-2017 11.00.00 AM' is sent from the other system (i want to convert this time to a specific TimeZone) and how to rewrite this query.
You already have everthing in you query, to convert a timestamp to a specific timezone you have to use "AT TIME ZONE" precising the timezone you want your timestamp to be converted:
select systimestamp AT TIME ZONE 'America/Los_Angeles' "West Coast Time" from dual;
I'm not sure what you want to accomplish and the timestamp you want to convert needs to include a timezone if you want to convert it to another timezone.
Related
I was inspecting how are DATEs stored in database. Consider the following statement:
SELECT
CAST (to_timestamp_tz('2018-12-05T10:00:00+01:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE) AS PRAGUE_TIME,
CAST (to_timestamp_tz('2018-12-05T10:00:00+00:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE) AS GMT_TIME
FROM DUAL
Results in:
PRAGUE_TIME GMT_TIME
2018-12-05 10:00:00 2018-12-05 10:00:00
The times are the same, despite one being created from a +1 timezone offset. Just to be sure, I added further conversion to string:
SELECT
TO_CHAR(CAST (to_timestamp_tz('2018-12-05T10:00:00+01:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE),'YYYY-MM-DD HH24:MI:SS') AS PRAGUE_TIME,
TO_CHAR(CAST (to_timestamp_tz('2018-12-05T10:00:00+00:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE),'YYYY-MM-DD HH24:MI:SS') AS GMT_TIME
FROM DUAL
PRAGUE_TIME GMT_TIME
2018-12-05 10:00:00 2018-12-05 10:00:00
Same results. So how can I convert TIMESTAMP to DATE without loosing the timezone information?
how can I convert TIMESTAMP to DATE without loosing the timezone information?
You cannot.
A DATE only has year, month, day, hour, minute and second components.
A TIMESTAMP has all those components plus nanoseconds and optionally time zone or time zone offset components.
If you convert from a TIMESTAMP to a DATE then you will lose the information that the DATE cannot store.
What you can do is convert all the TIMESTAMPs to the same time zone using, for example, AT TIME ZONE 'UTC':
SELECT CAST( TIMESTAMP '2018-12-05 10:00:00+01:00' AT TIME ZONE 'UTC' AS DATE )
AS PRAGUE_TIME_AS_UTC,
CAST( TIMESTAMP '2018-12-05 10:00:00+00:00' AT TIME ZONE 'UTC' AS DATE )
AS GMT_TIME_AS_UTC
FROM DUAL
Results:
| PRAGUE_TIME_AS_UTC | GMT_TIME_AS_UTC |
|----------------------|----------------------|
| 2018-12-05T09:00:00Z | 2018-12-05T10:00:00Z |
Please advise from review perspective is that below query will fetch up all the data with context to last date or still any modification is required as i am worried from the point of last day data , i want that it should bring also all the data with context to last day
SELECT PFT.*, PA.* FROM PFT_DATA PFT, CONTACT_TOKEN CT, PRODUCT_ATTRIBUTE PA, PERSON P WHERE PFT.PERSON_ID = ? AND PFT.TO_CONTACT_TOKEN_ID = CT.CONTACT_TOKEN_ID AND PFT.PRODUCT_ATTRIBUTE_ID = PA.PRODUCT_ATTRIBUTE_ID AND PFT.PERSON_ID = P.PERSON_ID AND PFT.CREATED_ON between to_date('05/24/2018', 'MM/dd/yyyy HH24:MI:SS') AND to_date('06/02/2018', 'MM/dd/yyyy HH24:MI:SS');
If pft.created_on has value for the time portion of the date, then no. Your to_date will give you 2018-06-02 00:00:00 and so a pft.created_on of 2018-06-02 00:00:01 (thru 23:59:59) would not be included.
Better to use 2018-06-02 23:59:59 as you already have the 'HH24:MI:SS' in your date mask.
And if pft.created_on happens to be a TIMESTAMP, you would be better off using TO_TIMESTAMP instead of TO_DATE.
Was wondering if anyone could help with precision time conversion.
Sample: 1501646399999 which is GMT: Wednesday, August 2, 2017 3:59:59.999 AM
I used the below query, but it always rounds off to 02-AUG-17 04:00:00. Can anyone please guide me
select TO_TIMESTAMP('1970-01-01 00:00:00.000', 'YYYY-MM-DD hh24:mi:SS.FF3') + ((1/86400000) * 1501646399999)
from dual;
The problem is that you're adding a number to your fixed timestamp, which is causing that timestamp to be implicitly converted to a date - which doesn't have sub-second precision.
If you add an interval instead then it stays as a timestamp:
alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS.FF3';
select TO_TIMESTAMP('1970-01-01 00:00:00.000', 'YYYY-MM-DD hh24:mi:SS.FF3')
+ numtodsinterval(1501646399999/1000, 'SECOND')
from dual;
TO_TIMESTAMP('1970-01-0
-----------------------
2017-08-02 03:59:59.999
Incidentally, you could slightly simplify your query with a timestamp literal:
select TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(...)
You may also want to check if you should be declaring that timestamp as being UTC, and converting back to local time zone after adding the epoch value; or leaving it explicitly as UTC but as a timestamp with time zone value. It depends exactly what that number is supposed to represent. (You said it's GMT/UTC, but still...)
I have a varchar2(20) column with a value like '2015-01-26T20:29:51Z'.
I successfully convert it to a date with to_date(dEnteredDate,'YYYY-MM-DD"T"HH24:MI:SS"Z"')
I wish to convert it to local time by subtracting my SessionTimeZone, currently -07:00 and have a regular date with time, no timezone info.
I do this in MS SQL with;
SET #diff = datediff(hh,GetUTCDate(), GetDate());
SET #dlocal = DATEADD(hh, #diff, #UTCDateTime)
How can I accomplish the same in Oracle 10g?
You can do it like this:
select to_timestamp(dEnteredDate,'YYYY-MM-DD"T"HH24:MI:SS"Z"')- interval '7' hour from test_3;
Here - interval '7' hour will subtract the required time difference and give you the desired result without timezone.
You can do it like this
select cast(to_timestamp_TZ(dEnteredDate||' UTC','YYYY-MM-DD"T"HH24:MI:SS"Z" tzr') at local as date)
from ...
We are using Oracle database.
In our table timestamp is stored as seconds since 1970, how can I convert the time stamp obtained through current_timestamp() function to seconds
This would do it:
select round((cast(current_timestamp as date) - date '1970-01-01')*24*60*60) from dual
Though I wouldn't use current_timestamp if I was only interested in seconds, I would use SYSDATE:
select round((SYSDATE - date '1970-01-01')*24*60*60) from dual
Maybe not completely relevant. I had to resolve other way around problem (e.g. Oracle stores timestamp in V$RMAN_STATUS and V$RMAN_OUTPUT) and I had to convert that to date/timestamp. I was surprised, but the magic date is not 1970-01-01 there, but 1987-07-07. I looked at Oracle's history and the closest date I can think of is when they ported Oracle products to UNIX. Is this right?
Here's my SQL
SELECT /*+ rule */
to_char(min(stamp)/(24*60*60) + date '1987-07-07', 'DD-MON-YYYY HH24:MI:SS') start_tm
, to_char(to_char(max(stamp)/(24*60*60) + date '1987-07-07', 'DD-MON HH24:MI:SS')) end_tm
FROM V$RMAN_STATUS
START WITH (RECID, STAMP) =
(SELECT MAX(session_recid),MAX(session_stamp) FROM V$RMAN_OUTPUT)
CONNECT BY PRIOR RECID = parent_recid ;
I needed to send timestamp to GrayLog via GELF from Oracle DB. I tried different versions and solutions but only one worked correctly.
SQL:
SELECT REPLACE((CAST(dat AS DATE) - TO_DATE('19700101', 'YYYYMMDD')) * 86400 + MOD(EXTRACT(SECOND FROM dat), 1), ',', '.') AS millis
FROM (SELECT SYSTIMESTAMP AT TIME ZONE 'GMT' AS dat FROM dual)
The result for Systmiestamp
2018/12/18 19:47:29,080988 +02:00
will be
1545155249.080988