This question already has answers here:
How to convert timestamp with milliseconds to date in Oracle
(1 answer)
oracle convert unix epoch time to date
(6 answers)
Closed 10 months ago.
What's the best way to convert an epoch column to UTC in the format yyyy-mm-dd hh24:mi:ss in Oracle db (PL/SQL).
e.g. 1646670198000 --> 2022-03-07 4:23:18
The main consideration is the data type of the result. Epoch is a timestamp at time zone GMT; the data type should be timestamp with time zone.
If we can agree on this, then here's one way:
select timestamp '1970-01-01 00:00:00 GMT'
+ 1646670198 * interval '1' second as ts
from dual
;
TS
----------------------------
2022-03-07 16:23:18.000 GMT
I wrote it that way - with the epoch set off separately as a multiplying factor - so that you can replace it with a column name, a bind variable, or any other expression that evaluates to a valid epoch in seconds.
Related
I have an issue importing date from a Tririga database into a SQL database. Mainly I cant convert the date properly and it looks like is not the commont format I have seen around.
Eg date value incomming 775724400000
Running something like select to_date('765187200000', 'DDMMYYYYHH24MISS') my_date FROM dual;
give me an error
ORA-01847: day of month must be between 1 and last day of month
01847. 00000 - "day of month must be between 1 and last day of month"
I found the following info from this link seems to be also from tririga
link_help
And the size of the number are about 10 digits meanwhile this one is 12 and I know for fact this dates should be from the past 10 years (most of them)
I can't seem to find anything that gives me an answer how to convert this into proper dates.
Any suggestions?
The input number appears to be "epoch", a count of milliseconds elapsed since 1 January 1970 GMT (UTC).
To convert to date is not difficult:
select date '1970-01-01' + (775724400000 / 86400000) as dt from dual;
DT
--------------------
1994-Aug-01 07:00:00
Note the hard-coded literals: date '1970-01-01' (epoch is by definition measured from midnight on this date) and 86400000. By one of the definitions (in the previous version of the International System of Units and Weights), a second is 1/86400 of a median day. In Oracle, date arithmetic is based on the number 1 representing one day, so to convert your milliseconds to days you must divide your input by 86400 * 1000.
The most delicate question has to do with time zones (and possibly daylight saving time, also related to time zone). In most cases, epoch is measured from midnight on 1 January 1970 GMT, not from midnight on 1 January 1970 in local time. Do you need to adjust for that? Only you and your business users can answer that question.
(As an aside, the number you provided does NOT represent a date in the past 10 years - not even close.)
This question already has an answer here:
Oracle conversion of UNIX timestamp to timestamp with time zone
(1 answer)
Closed 5 years ago.
How to convert unix_timestamp to timestamp in oracle?
I used the below function but it didn't work.
"UNIX_TIMESTAMP(event_time_stamp)"
This is the complete query :
enter image description here
The UNIX timestamp represents the number of seconds which have elapsed since January 1, 1970. Oracle allows adding some number of days directly to a timestamp. We can build the timestamp you want by adding the appropriate number of days in your UNIX timestamp value to 1970-01-01 00:00:00:
SELECT
TIMESTAMP '1970-01-01 00:00:00' + NUMTODSINTERVAL(1511421211, 'second')
FROM dual;
This returns the following:
23.11.2017 07:13:31
Demo
How can I change 'YYYY-MON-DD HH24:MI:SS' to Date Format (Not String) mm/dd/yyyy?
Sample: 2012-01-20 12:04:13.287 to 01/20/2012
Dates (and timestamps) do not have a format - they are represented internally by 7 or 8 bytes for a date or 20 bytes for a timestamp.
If you want to format a date or a timestamp then you will have to convert it to a string either explicitly with TO_CHAR():
SELECT TO_CHAR( date_column, 'MM/DD/YYYY' )
FROM your_table;
Or implicitly within whatever client program (i.e. you've tagged plsqldeveloper) you are using - in which case check the preferences within your IDE.
I am trying to convert a date field 'createdate' to UTC time but the ask is to be -04:00. I have tried a bunch of things and have had no success. I am working on Oracle. Any ideas? Thank you.
This should do it:
cast(createdate as timestamp with time zone) at time zone 'UTC'
This converts the createdate to a timestamp with your current time zone (the one that is defined by your client through SESSIONTIMEZONE). It then converts that to UTC.
It is not possible if you have a DATE or TIMESTAMP value, because those data types do not have any time zone information and thus it is not possible to convert to any other time zone - unless you treat the value as "local time zone".
There are several solutions:
createdate at time zone 'UTC'
SYS_EXTRACT_UTC(createdate)
FROM_TZ(createdate, 'UTC')
The result types are different, e.g. FROM_TZ returns as TIMESTAMP WITH TIME ZONE values, whereas SYS_EXTRACT_UTC returns a TIMESTAMP value.
What are the difference between Oracle DATE and TIMESTAMP type?
Both have date and time component?
Also what is corresponding type in Java for these date types?
DATE and TIMESTAMP have the same size (7 bytes). Those bytes are used to store century, decade, year, month, day, hour, minute and seconds. But TIMESTAMP allows to store additional info such as fractional seconds (11 bytes) and fractional seconds with timezone (13 bytes).
TIMESTAMP was added as an ANSI compliant to Oracle. Before that, it had DATE only.
In general cases you should use DATE. But if precision in time is a requirement, use TIMESTAMP.
And about Java, the oracle.sql.DATE class from Oracle JDBC driver, provides conversions between the Oracle Date/Timestamp data type and Java classes java.sql.Date, java.sql.Time and java.sql.Timestamp.
Illustration of datetime data types in Oracle:
and supporting functions: