Oracle converting to Eastern Time Zone - oracle

I am running an API call that runs the following Oracle code for DAO, and suppose to bring back the latest banner scheduled by DT/TM.
String query = "SELECT * FROM BANNER_MSGS WHERE from_tz(cast(SYSDATE as timestamp), 'GMT') at time zone 'US/Eastern' BETWEEN SCHEDULE_FROM AND SCHEDULE_TO order by Banner_ID DESC FETCH FIRST 1 ROWS ONLY";
This works all day on my localhost but when moved to DEV server it will simply not work. Any Oracle gurus have some wisdom for the SQL syntax.

Related

Oracle PL/SQL How to get time in Paris?

What is the best way to always get the right time in Europe/Paris (including summer/winter times) ?
So far, I'm using the following query :
select FROM_TZ(CAST(Sysdate AS TIMESTAMP), 'Europe/Paris') from dual
It was working nicely in a previous server. However, we have changed our database server and now we are getting a difference of one hour.
Is there any way to get always the correct time in Paris ?
Thanks.
SYSDATE is returned in the time zone of database server's operating system. When you run FROM_TZ(CAST(Sysdate AS TIMESTAMP), 'Europe/Paris') then you "attach" time zone Europe/Paris to SYSDATE.
So, FROM_TZ(CAST(Sysdate AS TIMESTAMP), 'Europe/Paris') is only correct if the time zone of database server's operating system is Europe/Paris.
Try SYSTIMESTAMP AT TIME ZONE 'Europe/Paris' then result is always correct because SYSTIMESTAMP returns a TIMESTAMP WITH TIME ZONE value and time zones are properly converted.

Different timestamps for same sql query in Source Oracle Database and Target same Oracle Database while using Informatica

The source is having Createddate as 17-JAN-18 17.22.39.000000000 in my table.
When I execute the following query:
select CAST(Createddate AT TIME ZONE 'UTC' AS Timestamp) from Employee
on Oracle Database, It is giving below results.
17-JAN-18 15.22.39.000000000
But when I use the same SQL Query in Source Qualifier(Informatica) the target is getting below result which is in the same Oracle Database.
17-JAN-18 12.22.39.000000000
I am getting a 1 hour difference. Can someone help how can I get the same timestamp?
I assume your Createddate value is a TIMESTAMP value (without TIME ZONE).
When you run CAST(Createddate AT TIME ZONE 'UTC' AS Timestamp) then Oracle actually does
CAST(
FROM_TZ(Createddate, SESSIONTIMEZONE)
AT TIME ZONE 'UTC' AS Timestamp)
I.e. it appends the current session time zone to the timestamp value and then it is converted to new time zone.
So, the session time zones are different. The most reliable solution would be to set the time zone explicitly, e. g.
CAST(FROM_TZ(Createddate, 'Europe/Zurich') AT TIME ZONE 'UTC' AS Timestamp)
btw, for your function there is a shortcut, simply use like
SYS_EXTRACT_UTC(FROM_TZ(Createddate, 'Europe/Zurich'))

Oracle server timezone using SQL query

I ran
select SYSDATE from dual;
Output:
SYSDATE |
-------------------|
2019-10-09 08:55:29|
Then I ran,
SELECT DBTIMEZONE FROM DUAL;
Output:
DBTIMEZONE|
----------|
+00:00 |
In the first output, time is in EST and 2nd output suggests timezone is UTC.
How do I check oracle server timezone via SQL query?
From the docs:
The database time zone [DBTIMEZONE] is relevant only for TIMESTAMP WITH LOCAL TIME ZONE columns. Oracle recommends that you set the database time zone to UTC (0:00)...
SYSDATE/SYSTIMESTAMP will return the time in the database server's OS timezone. Selecting a TIMESTAMP WITH LOCAL TIME ZONE datatype will return the time in your session's timezone (ie, SESSIONTIMEZONE).
select
CAST(systimestamp AS timestamp(0) with local time zone) as local_time,
systimestamp as server_time
from dual;
DBTIMEZONE is only used as the base timezone stored in TIMESTAMP WITH LOCAL TIME ZONE columns - which you never see, because when you select from one of those columns it gets translated into your session timezone.
See this similar question for a very detailed answer.
It is a common misunderstanding that SYSDATE or SYSTIMESTAMP are returned at DBTIMEZONE
SYSDATE and SYSTIMESTAMP are given in the time zone of database server's operating system. If you like to interrogate the time zone of database server's operating system run
SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual;
see also How to handle Day Light Saving in Oracle database

Timestamp variations between oracle and postgres

Currently, I am working on migrating an Oracle database schema to Postgres. As part of it I have to convert a custom utility function in Oracle which generates timestamps based on timezone. But the values obtained from Oracle and Postgres seem to vary by one hour.
Oracle:
SELECT (to_timestamp_tz('20300714 235959 CET','YYYYMMDD HH24MISS TZR') - to_timestamp_tz('19700101 000000 GMT','YYYYMMDD HH24MISS TZR'))
as foo FROM dual;
yields +22109 21:59:59.000000
Postgres:
select ('20300714 235959'::timestamp AT TIME ZONE 'CET') - ('19700101 000000'::timestamp AT TIME ZONE 'GMT') as foo;
yields 22109 days 22:59:59
I guess the reason for this difference is because of daylight saving but I am not sure. Can anyone help me out with this problem.
I am using Postgres v9.6 and Oracle 12c.
Well, I found the mistake I was doing. Postgres seems to handle the abbreviated timezone name and full timezone name differently. In case if you want to incorporate daylight related changes you would have to use full timezone name. In my case using 'Europe/Amsterdam' instead of 'CET' yielded the same result as that of Oracle.
To find the complete list of full timezone names supported by Postgres, I used the query:
select * from pg_timezone_names where abbrev='CET';

Database column with datatype TIMESTAMP(6) WITH LOCAL TIME ZONE Issue

I am running a report from the jasperserver application which is not getting the correct data into the report based on the time i want the data.
The query that is running on the database is fetching the data based on the time see query below.
select * from intot where Site = 'LYNDANE' and code = 'RC1'
and stamp between TO_DATE('2016/09/22 06:00:00', 'YYYY/MM/DD HH:MI:SS')
and TO_DATE('2016/09/22 08:00:00', 'YYYY/MM/DD HH:MI:SS') order by stamp asc;
it is fetching the incorrect data for below time
2016/09/22 07:00:00 and 2016/09/22 09:00:00
Now when i run the query for one hour back it is fetching the correct data.
select * from intot where Site = 'LYNDANE' and code = 'RC1'
and stamp between TO_DATE('2016/09/22 05:00:00', 'YYYY/MM/DD HH:MI:SS')
and TO_DATE('2016/09/22 07:00:00', 'YYYY/MM/DD HH:MI:SS') order by stamp asc
I checked the intot structure and found that the stamp is of datatype
TIMESTAMP(6) WITH LOCAL TIME ZONE
I am suspecting that the query i am sending to the DB for running converts the date according to the time zone.
I checked the timezone of the database see below
select dbtimezone from dual;
DBTIME
------
+00:00
Is it a way i can amend the query to get the correct data always and do not get affected by the Day light shifting.

Resources