Converting NUMBER to DATE - sql

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

Related

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

How to convert unix timestamp to date with time zone?

How can I convert for example timestamp:
1559347196759
to datetime with time zone 'GMT+2' using PostgreSQL?
I believe this does what you want:
select (timestamp 'epoch' + 1559347196759 * interval '1 millisecond') at time zone '+02:00'
The first expression converts the value to UTC. The second shifts the timezone.
Convert it to a timestamp with time zone (absolute time) and convert it to the appropriate time zone:
SELECT to_timestamp(1559347196759 / 1000.0) AT TIME ZONE '-02';
timezone
-------------------------
2019-06-01 01:59:56.759
(1 row)

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

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

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

new_time returning time in 12hrs format

In my function I want to convert time from GMT to BST.
My query is like below
SELECT TO_CHAR(NEW_TIME ((TRUNC(SYSDATE) + 20/24 + 21/1440),
'GMT',
'BST'),
'HH24:MI'
)
FROM DUAL;
Its returning me '9:21' instead of '21:21'.
Please help! Thank you!
From the documentation, BST refers to Bering Standard Time, and not British Summer Time as you are expecting.
Bering Standard Time is at UTC-11, so the result that you get is as expected.
NEW_TIME function can accept only limited timezones specified in the above documentation.If you want to convert GMT to British Summer Time,
you should probably use 'Europe/London' timezone.
select (cast(your_date as timestamp) at time zone 'GMT') at time zone 'Europe/London'
from dual;
So your query will be,
SQL> select
(cast((TRUNC(SYSDATE) + 20/24 + 21/1440) as timestamp) at time zone 'GMT') at time zone 'Europe/London',
to_char((cast((TRUNC(SYSDATE) + 20/24 + 21/1440) as timestamp) at time zone 'GMT') at time zone 'Europe/London','hh24:mi')
from dual;
(CAST((TRUNC(SYSDATE)+20/24+21/1440)ASTIMESTAMP)ATTIMEZONE'GMT')ATTIMEZONE' TO_CH
--------------------------------------------------------------------------- -----
14-MAY-14 09.21.00.000000 PM EUROPE/LONDON 21:21