convert a string to oracle sql timestamp Mon Aug 19 2019 08:21:48 GMT-0700 (PDT) - sql

Greeting Community.
I saw similar examples in Java but how do we do it it SQL in particular Oracle.
For now I just do this which it works because we are in PDT timezone.
-Thx for the help!
with xx as (
select replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)',' GMT-0700 (PDT)','') as dt from dual
)
select to_date(dt,'DY MON DD YYYY HH24:MI:SS') from xx

The input is a timestamp with time zone. It has redundant information about the time zone; some of that must be stripped away. For example, if you choose to trust PDT and ignore the GMT offset, you could do something like this:
with xx as (
select regexp_replace('Mon Aug 19 2019 08:21:48 GMT-0700 (PDT)','GMT-\d{4} \(|\)')
as str_ts_with_tz
from dual
)
select to_timestamp_tz(str_ts_with_tz,'Dy Mon DD YYYY HH24:MI:SS TZD')
as oracle_ts_with_tz
from xx
;
ORACLE_TS_WITH_TZ
---------------------------------------
2019-08-19 08:21:48 America/Los_Angeles
Note that the resulting data type is timestamp with time zone. You can further cast this as timestamp or as date (simply discarding the time zone information) if needed; this is a simple operation, internal to Oracle. But that will lose information; think twice before you do that.
Note also that the timestamp is presented in a different format in my output, compared to your input. This is because my NLS_TIMESTAMP_TZ_FORMAT is 'yyyy-mm-dd hh24:mi:ss tzr'.
If you are wondering why you must make a choice (which, then, means "why you need to delete part of the input string first") regarding the time zone information: PDT is simply not the same as GMT-0700. It may be that in summer, but not in winter. Oracle won't accept such self-contradicting nonsense as input to its functions. Either it's GMT-0700 or it's PDT, it can't be both. And, you can't just use REPLACE (not easily, anyway), because what must be replaced may have variable characters in it.

Related

How do you convert timestamp with GMT timezone in Redshift?

How do you convert a string with DOW and GMT information to timestamptz in Redshift?
Example: Mon Apr 01 2019 14:08:20 GMT-0400 (EDT)
Amazon Redshift is based on PostgreSQL. Therefore, with the use of TO_DATE function, along with timezone related clauses, we could try the following.
SELECT
TO_TIMESTAMP('Mon Apr 01 2019 14:08:20 GMT-0400 (EDT)', 'XXX Mon DD YYYY HH:MI:SS')
AT TIME ZONE REGEXP_REPLACE('Mon Apr 01 2019 14:08:20 GMT-0400 (EDT)', '^.*(...).$', '\\1');
This results in the following.
Unfortunately, it doesn't seem like we can do this conversion in one go because time zone related format flags are valid for TIMESTAMPTZ only.

varchar2 string to Date format

I am trying to convert the below string value to TO_DATE but oracle is not
recognizing the date format. Where am I going wrong.
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC','Dy Mon DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE = American')
FROM dual;
ERROR at line 1:
ORA-01821: date format not recognized
Use:
SELECT TO_TIMESTAMP_TZ('Wed Oct 10 23:50:00 2018 UTC','DY MON DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE=American')
FROM dual;
TO_DATE function returns DATE datatype, which does not support timezones. You are using TZR format specifier in your query (time zone region), and this generates the error because DATE does not support this.
This documentation - Time Zones shows which datatypes supports timezones, and states that only TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE do. So you must convert to one of this datatype instead of converting to DATE.
TO_TIMESTAMP_TZ function converts a literal to TIMESTAMP WITH TIME ZONE datatype.
If (and I realise it's a big 'if') the string values always contain UTC and not any other time zone values, then you could just treat that as a character literal. You would do that by changing your format model from TZR, which isn't recognised by to_date(), to "UTC" - including the double quotes:
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC',
'Dy Mon DD HH24:MI:SS YYYY "UTC"',
'NLS_DATE_LANGUAGE = American')
FROM dual;
TO_DATE('WEDOCT1023
-------------------
2018-10-10 23:50:00
Of course, as that is a plain date it still has no time zone information, but if you want to retain that then you need a timestamp with [local] time zone data type anyway.

Cannot convert PSQL string to timestamp

I am trying to do a simple string to date conversion; however, PSQL complains when there is a timezone in that string. Their documentation clearly states that its supported; however, it complains. I don't even care about the timezone, I just want to convert the string.
db=> select to_timestamp('Mon Feb 23 13:43:07 PST 2015', 'Dy Mon DD HH24:MI:SS TZ YYYY')::timestamp without time zone;
ERROR: "TZ"/"tz" format patterns are not supported in to_date
Postgres version: 9.3.10
http://www.postgresql.org/docs/9.3/static/functions-formatting.html
Why not try a straight cast?
SELECT 'Mon Feb 23 13:43:07 PST 2015'::timestamp with time zone
Once you've done this it's pretty easy to work with the date object.

Converting a timestamp in milliseconds in Postgres

I have a timestamp in milliseconds:
1420066991000
That translates into UTC:
Wed Dec 31 2014 23:03:11
and local time:
Thu Jan 01 2015 00:03:11
However if I try to convert it into a timestamp in Postgres, using to_timestamp, it gives me a wrong datetime:
select to_timestamp(1420066991000);
46970-02-17 13:03:20+00
Since to_timestamp, expects a double precision input, I also did this:
select to_timestamp(1420066991000.0);
46970-02-17 13:03:20+00
but the results are the same.
Am I missing something in my Postgres configuration, like some timezone setting? or is it a bug?
to_timestamp() converts unix time, that is time in seconds. Since you have data in miliseconds you should divide it by 1000.
select to_timestamp(1420066991000/1000);

Load File to Oracle TIMESTAMP(6) Format

I'm trying to load FLATFILE data to Oracle via Sqlloader, but I'm having trouble with a timestamp(6) field.
I have timestamp values in the below format:
Wed May 08 00:00:00 UTC 2013
I need sqlloader code to load this time format in oracle.
My current sqlloader code is below
LOAD DATA
INFILE 'MYDIR/my_documents_data.dat'
TRUNCATE
INTO TABLE sample_table
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
"MYID" INTEGER EXTERNAL,
"NAME" CHAR(100),
"DOCUMENTTYPE" CHAR(250),
"DATEADDED" TIMESTAMP,
"DOCUMENTSIZE" INTEGER EXTERNAL
)
Please advise me which format should I apply for this column "DATEADDED" TIMESTAMP, which is actually a timestamp(6).
Wed May 08 00:00:00 UTC 2013
Given that sample, the proper format is probably:
DY MON DD HH24:MI:SS TZR YYYY
See http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm#SQLRF00212 for the detail of the various fields.
You have to load your data as a timestamp with timezone though, because I used TZR to map the UTC part of your data. Something like that:
...
"DATEADDED" TIMESTAMP WITH TIME ZONE "DY MON DD HH24:MI:SS TZR YYYY"
...
If you really want a timestamp without timezone, use that format instead:
DY MON DD HH24:MI:SS "UTC" YYYY
This will assume that all your timestamps are UTC and will silently discard that information.
...
"DATEADDED" TIMESTAMP "DY MON DD HH24:MI:SS "UTC" YYYY"
...