Timestamp to complete date format - oracle

We are working on oracle 11g enterprises edition. We are facing issue in getting value of date for type TIMESTAMPTZ and TIMESTAMPLTZ. We are storing those dates into one csv file using apache metamodel. We are fetching date from database using :
TIMESTAMPLTZ columnValue = (TIMESTAMPLTZ) row.getValues()[pos];
Timestamp timestamp=columnValue.timestampValue(connection,Calendar.getInstance(Locale.getDefault()));
Date dateByTimeStamp=new Date(timestamp.getTime());
Date dateByDateValue = columnValue.dateValue(connection);
String formattedDate = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS a").format(dateByTimeStamp or dateByDateValue );
dateByDateValue contains date upto yyyy-mm-dd hh:mm:ss format. eg. 2016-10-12 08:49:30. There is no way to get value of nanoseconds and time zone using this object.
dateByTimeStamp object contains cdate that have value of date upto nanoseconds and also have time zone. But the value of date time differ because of time zone, but this cdate object contains information regarding nanoseconds precision.
Is there any way to get date in complete format so that the formatted date can directly be used to restore into another oracle db?

Related

Get the original Timezone from Postgres tstz field

I am having a table like this.
CREATE TABLE tstz (
ts timestamp NULL,
tstz timestamptz NULL,
seq text NULL
);
Whenever, I store the timestamp with time zone, it gets converted to the timezone of the database. How do I get back the original timezone with which it is retrieved?
For example, I am storing the timezone like 2020-04-29T08:06:03.424689+05:30 to the database. However, when I read the database using Java and read the timezone again like following.
Optional<Tstz> tstzOptionalSaved = tstzRepository.findById(tstz.getSeq());
Tstz tstzSaved = tstzOptionalSaved.get();
log.info("Timezone {}" , tstzSaved.getTstz().getZone().getId());
the above code always returns UTC which is the timezone of the database.
Is there a way to do this in Postgres or in Java?
You can use OffsetDateTime. From postgresql docs,
all OffsetDateTime will instances will have to be in UTC (have offset 0). This is because the backend stores them as UTC.
So you need to add one column for offset and then use the offset to get original value,
OffsetDateTime tstz = rs.getObject("tstz", OffsetDateTime.class);
OffsetDateTime odtOriginal = tstz
.toInstant()
.atOffset(ZoneOffset.ofTotalSeconds(rs.getInt("tstz_offset")));
// 2020-04-29T08:06:03.424689+05:30

How to fetch the date from epoch in CockroachDB

I have a column which has the time stamp stored in epoch. I need to fetch the date from the epoch time stamp. For instance for epoch = 1552942715, I need to fetch the date as 1552867200. How can this be achieved?
create table test (epoch text NULL);
Create table sample where the column is text datatype.
select (epoch::int)::TIMESTAMPTZ from test;
If the column is of type int or any other number then just cast it to timestampz

How to store timestamp with timezone

I am trying to store a timestamp with timezone into my oracle db.
I have for example this code:
$deadline = new \DateTime('2018-11-07 13:33', new \DateTimeZone("EUROPE/BERLIN"));
$control->setDeadline($deadline);
and the result stored in my oracle db is this one:
07.11.18 13:33:00.000000000 +01:00
but my goal is to to store the timestamp with this format: 07.11.18 13:33:00.000000000 EUROPE/BERLIN
if I run this query the value is correctly saved with the desired format:
update my_table
set deadline = TIMESTAMP '2018-11-07 09:00:00 EUROPE/BERLIN'
What I am doing wrong? What is the correct way to format the timestamp to obtain the desired result with symfony?
Looks like synfony transforms Europe/Berlin to +01:00 - which would be a bug.
As workaround you could do following:
ALTER SESSION SET TIME_ZONE = 'Europe/Berlin';
and then insert the value without any time zone, i.e.
$deadline = new \DateTime('2018-11-07 13:33');
$control->setDeadline($deadline);
If you insert a timestamp into a TIMESTAMP WITH TIME ZONE column and you don't provide any time zone information then Oracle defaults the time zone to your current SESSIONTIMEZONE which you set before.
You can set your SESSIONTIMEZONE also by Environment Variable ORA_SDTZ or in your Registry at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ORACLE\KEY_{ORACLE_HOME Name}\ORA_SDTZ, resp. HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_{ORACLE_HOME Name}\ORA_SDTZ

Timestamp field into oracle by using sqlloader

I am trying to load this source row into oracle
Source row:
1000-01-01 00:00:00:000000
by using this CTL statement;
entrydate TIMESTAMP "YYYY-MM-DD hh:mi:ss:ff6"
but I am getting
ORA-26041: DATETIME/INTERVAL datatype conversion error
What am I doing wrong?
Timestamp format or data problem hh - Hour of day (1-12). hh24 - Hour of day (0-23).
TIMESTAMP "YYYY-MM-DD hh24:mi:ss:ff6" try this version.

Convert UTC date to oracle Date

I have a string in this format: "2013-06-05T19:41:12.739" and I need to convert it to a date field in this format: "2013-06-05 19:41:12"
How can I do this with Oracle?
You use to_date() or to_timestamp() to convert a string literal to a date/timestamp value:
If you need the milliseconds you have to convert it into a timestamp, otherwise (if you want to discard the milliseconds) you can convert it into a date:
select to_timestamp('2013-06-05T19:41:12.739', 'yyyy-mm-dd"T"hh24:mi:ss.ff3')
from dual;
To get rid of the milliseconds, simply cast the result from the above statement to a DATE
select cast(to_timestamp('2013-06-05T19:41:12.739', 'yyyy-mm-dd"T"hh24:mi:ss.ff3') as date)
from dual;
I need to convert it to a date field in this format:
A DATE column does NOT have a "format".
You apply a format to a DATE column when you display it. Either explicitely by using to_char() or implicitely by the NLS settings in effect (or by some code in your application).

Resources