I want to adjust a date for summer/winter time and time zone when insterting it into a table - sql

I have a date and time variable in TABLE_A that is in GMT. I want to insert this date and time into TABLE_B, but I want the insterted value to be adjusted for time zone and summer/winter time.
That is:
INSERT into TABLE_A (ADJUSTED_DATE_AND_TIME)
SELECT GMT_DATE_AND_TIME [Perform proper adjustments here..?]
FROM TABLE_A
Can I do this? In that case, how do I write ?
Thank.

I think you can simply convert the GMT/UTC time. However, you have to take the full region name of your time zone.
SELECT TIMESTAMP '2014-06-10 12:00:00 +00:00' AT TIME ZONE 'Europe/Zurich' AS summer FROM dual;
SUMMER
---------------------------------------
10.06.2014 14:00:00.000000000 +02:00
SELECT TIMESTAMP '2014-12-10 12:00:00 +00:00' AT TIME ZONE 'Europe/Zurich' AS winter FROM dual;
WINTER
---------------------------------------
10.12.2014 13:00:00.000000000 +01:00
Since your source value is data type DATE you have to do following steps.
Cast DATE to TIMESTAMP
Set Time zone of the value using FROM_TZ
Convert the value to new time zone using AT TIME ZONE '...'
Cast the value to DATE
Written in a single statement it is
select
CAST(FROM_TZ(CAST(sy_sttime AS TIMESTAMP), 'UTC') AT TIME ZONE 'Europe/Zurich' AS DATE)
from sy_request
or a bit less clear
select
CAST((CAST(sy_sttime AS TIMESTAMP) AT TIME ZONE 'UTC') AT TIME ZONE 'Europe/Zurich' AS DATE)
from sy_request

Related

Combining date and UTC time fields in Postgres

I have 2 separate fields for date and time. The time field is stored in UTC time. How can I combine the 2 into a datetime field into local time
Example:
date: 2021-03-08
time in UTC: 23:00
time zone: GMT+8
I would like to get 2021-03-08 07:00 in local time
or even 2021-03-07 23:00 in UTC
Note: Combining the fields is not an option unfortunately.
Need to know your time zone to convert. To convert utc to america/los_angeles time zone:
select '2021-03-08 23:00'::timestamp at time zone 'UTC' at time zone 'america/los_angeles'
you can check out below codes:
If you have a timestamp without time zone column and you're storing timestamps as UTC, you need to tell PostgreSQL that, and then tell it to convert it to your local time zone.
select created_at at time zone 'utc' at time zone 'america/los_angeles'
from users;
To be more concise, you can also use the abbreviation for the time zone:
select created_at at time zone 'utc' at time zone 'pst'
from users;
To see the list of time zones PostgreSQL supports:
select * from pg_timezone_names;
SInce the time difference is 8 hours, try
SELECT '2021-03-08'::date + ('23:00'::time + '8 hours'::interval);
If you want this to work with arbitrary time zones, the query becomes more complicated:
SELECT '2021-03-08'::date
+ ((current_date + '23:00'::time)
AT TIME ZONE 'UTC'
AT TIME ZONE 'Asia/Ulaanbaatar'
)::time;

Casting Local Time to UTC Formating Incorrect

HIRE_DATE is in a 'DATE' column. The timestamp is local (Los Angeles); I would like to convert it to UTC.
I can't for the life of me fathom why the UTC output is mangled (Last 2 digits of YY is the DD; and vice-versa) -- and the time does not convert to UTC.
HIRE_DATE: 30/04/2019 12:00:00 AM
select from_tz(to_timestamp(HIRE_DATE,'DD-MM-YY HH24:MI:SS'), 'America/Los_Angeles') at time zone 'UTC' from TABLE
OUTPUT: 19/04/2030 12:00:00 AM
If HIRE_DATE is a DATE data type then you don't need TO_TIMESTAMP.
TO_TIMESTAMP is used to convert a string (i.e. VARCHAR2) into a TIMESTAMP value but you have a DATE value.
Just do
select from_tz(CAST(HIRE_DATE AS TIMESTAMP), 'America/Los_Angeles') at time zone 'UTC'
from TABLE
Actually I don't understand why FROM_TZ does not accept DATE values whereas almost any other date/timestamp related function accept either DATE or TIMESTAMP value as input.
Note, the default output display format of this query is defined by current user session NLS_TIMESTAMP_TZ_FORMAT setting. If you are not satisfied with the output format, either change NLS_TIMESTAMP_TZ_FORMAT setting by executing ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = '...' or use TO_CHAR function to set output format explicitly.
Instead of
... AT TIME ZONE 'UTC'
you can also use
SYS_EXTRACT_UTC(...)
The upper returns a TIMESTAMP WITH TIME ZONE value, the second one returns a TIMESTAMP value.
Would this do any good?
SQL> select from_tz(cast (sysdate as timestamp), 'UTC') result from dual;
RESULT
---------------------------------------------------------------------------
27.09.20 10:59:28,000000 UTC
Or, in your case
select from_tz(cast (hire_date as timestamp), 'UTC' from dual
No need to apply any format mask to hire_date as it is a DATE datatype (at least, that's what you said).
You use the word "convert" which can mean one of two things:
change the data type, which is what FROM_TZ does
change the value from one time zone to another, which FROM_TZ does not do.
You didn't give your expected output, so we may misunderstand.
To change the data type:
with data(dte) as (
select date '2019-04-30' + interval '12' hour from dual
)
select from_tz(cast(dte as timestamp), 'America/Los_Angeles') from data
FROM_TZ(CAST(DTEASTIMESTAMP),'AMERICA/LOS_ANGELES')
30-APR-19 12.00.00.000000 PM AMERICA/LOS_ANGELES
To get the simultaneous datetime value in UTC:
with data(dte) as (
select date '2019-04-30' + interval '12' hour from dual
)
select cast(sys_extract_utc(from_tz(cast(dte as timestamp), 'America/Los_Angeles')) as date) from data
CAST(SYS_EXTRACT_UTC(FROM_TZ(CAST(DTEASTIMESTAMP),'AMERICA/LOS_ANGELES'))ASDATE)
2019-04-30 19:00:00

ORA-01857: not a valid time zone

I am trying to convert from UTC time zone to GMT time zone.
I ran this below query and getting ORA error.
select NEW_TIME(SYSDATE, 'UTC', 'GMT') from dual;
And error is
Error starting at line : 1 in command -
select NEW_TIME(SYSDATE, 'UTC', 'GMT') from dual
Error report -
ORA-01857: not a valid time zone
I googled and find that NEW_TIME function is not accepting UTC time zone.
So, Can you please suggest me alternate solution/any way to convert from UTC to GMT?
UTC is also known as GMT, the latter which NEW_TIME already accepts. So, what you are trying to is equivalent to:
SELECT NEW_TIME(SYSDATE, 'GMT', 'GMT')
FROM dual;
The call to NEW_TIME doesn't make any sense of course. Check here for a list of accepted timezone codes.
Use FROM_TZ from convert a timestamp without a time zone to a timestamp with time zone (i.e. UTC) and then use AT TIME ZONE 'GMT' to convert it from the first time zone to the GMT time zone. You'll need to use CAST in various places as FROM_TZ expects a TIMESTAMP rather than a DATE and then you need to cast back to a DATE at the end (assuming you don't want a TIMESTAMP value):
SELECT CAST(
FROM_TZ(
CAST( SYSDATE AS TIMESTAMP ),
'UTC'
)
AT TIME ZONE 'GMT'
AS DATE
) As gmt_time
FROM DUAL
Output:
| GMT_TIME |
| :------------------ |
| 2019-04-10T14:05:37 |
db<>fiddle here

How to convert a column timezone from CET to CST in SQL query

Here I am trying to convert date column value from CET to CST.
I tried using NEW_TIME(SYSDATE ,'CET','CST') function, but it's giving an error saying unknown time zone. Issue here is CET is not recognised by oracle as valid timezone.
I tried using "at timezone" approach initially, but it's inserting the timezone name in the column value, which I don't want.
The three-character timezones don't adjust for daylight savings. To do that you need to use the fully specified timezone name. To illustrate, in Canada, there is central standard time, but the province of Saskatechewan does not use daylight savings, so if I want to convert right now to local time in summer where daylight savings is in play, and knowing the correct fully specified timezone names in the DB Timezone file (You can check the list installed in your DB by select * from V$TIMEZONE_NAMES;):
SELECT 'Central' locale
, extract(timezone_abbr from cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Canada/Central') tz_abbrv
, CAST(cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Canada/Central' as timestamp) local_time from dual
union all
SELECT 'Saskatchewan' locale
, extract(timezone_abbr from cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Canada/Saskatchewan') tz_abbrv
, CAST(cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Canada/Saskatchewan' as timestamp) local_time from dual;
LOCALE TZ_ABBRV LOCAL_TIME
Central CDT 03/08/2016 9:57:24.000000 AM
Saskatchewan CST 03/08/2016 8:57:24.000000 AM
Otherwise you would need to code when to switch between CST and DST in your calculations - bearing in mind that the rules for when daylight savings starts and ends has changed over time, and may change again in the future.
So respond to your comment about inserts, you need to first ensure that the column is defined to include the time zone (datatype "timestamp with timezone"), and change the CAST from "timestamp" to "timestamp with time zone" to make sure that the zone information is stored:
e.g.)
create table mbt (stz timestamp with time zone)
insert into mbt values (CAST(cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Indian/Maldives' as timestamp with time zone) )
insert into mbt values (CAST(cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Canada/Eastern' as timestamp with time zone) )
commit;
select * from mbt;
STZ
05/08/2016 8:06:49.000000 PM +05:00
05/08/2016 11:06:50.000000 AM -04:00
If you don't include the WITH TIME ZONE you should still get the changed value, but you won't be able to easily translate from local time to a different timezone as the values will be assumed to be in the server timezone:
drop table mbt;
create table mbt (stz timestamp);
insert into mbt values (CAST(cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Indian/Maldives' as timestamp) );
insert into mbt values (CAST(cast(add_months(sysdate,6) as timestamp) AT TIME ZONE 'Canada/Eastern' as timestamp ) );
commit;
select * from mbt;
STZ
-------------------------------
05-AUG-16 08.13.16.000000 PM
05-AUG-16 11.13.16.000000 AM
To remove timezone you can use cast(.... as Timesamp).
Check my example.
select cast( current_timestamp at time zone 'CST' as timestamp), cast( current_timestamp at time zone 'CST' as timestamp with time zone) from dual;

Converting NUMBER to DATE

I have a numeric field in my Oracle database that represents the date.
I'm not so familiar with Oracle commands.
I was wondering if anyone could provide some guide here.
Thanks.
example: 1435755908 = 7/1/2015 9:05
This is a Unix Timestamp, i.e. the seconds since January 1970, try this formula:
timestamp '1970-01-01 00:00:00' + 1435755908/86400
Since there seems to be a time zone difference:
select date '1970-01-01' + 1435755908/86400 as converted from dual;
CONVERTED
----------------------------------------
2015-07-01 13:05:08
you seem to need to do some time zone manipulation. Epoch times are UTC so you can use from_tz to declare that, and then at time zone to get the US/East Coast equivalent:
select from_tz(cast(date '1970-01-01' + 1435755908/86400 as timestamp), 'UTC')
at time zone 'America/New_York' as converted from dual;
CONVERTED
----------------------------------------
2015-07-01 09:05:08.000 AMERICA/NEW_YORK
Which is a time stamp with time zone. If you want it as a plain date then cast it:
select cast(from_tz(cast(date '1970-01-01' + 1435755908/86400 as timestamp), 'UTC')
at time zone 'America/New_York' as date) as converted from dual;
CONVERTED
----------------------------------------
2015-07-01 09:05:08