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.
Related
I have a column filled with dates in string format, e.g. 2023-01-31 11:21:33 GMT.
I am trying to write a query that will select a year and a month and will do some calculations later on. My standard approaches using EXTRACT(YEAR FROM a)) etc. did not work. Therefore, I am trying to parse datetime using PARSE_DATETIME(a, 'yyyy-mm-dd hh:mm:ss'). The thing is, I don't know how to format "GMT" and google did not help with that.
The error message is INVALID_FUNCTION_ARGUMENT: Invalid format: "2023-01-31 11:21:33 GMT" is malformed at "GMT".
Use 'yyyy-MM-dd HH:mm:ss z':
select parse_datetime('2023-01-31 11:21:33 GMT', 'yyyy-MM-dd HH:mm:ss z')
Output:
_col0
2023-01-31 11:21:33.000 UTC
parse_datetime is Java date function which uses JodaTime’s DateTimeFormat pattern format which is mostly compatible with java.text.SimpleDateFormat with z matching general timezone.
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.
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.
If I have dates that are in this format: Sep-29-07 13:45:00 PDT
How would I convert this into a Date object in oracle 11g? I have tried this:
SELECT TO_DATE(PublishDate, 'MON-DD-YY HH24:MI:SS')
FROM Order;
But I am getting an error saying:
ORA-01830: date format picture ends before converting entire input string
If you have only one time zone's dates, you can just ignore the timezone part using:
select to_date(PublishDate, 'MON-DD-YY HH24:MI:SS "PDT"')
from Orders;
If you plan to support multiple timezones, then store them with timestamp with time zone format. use correct timezone abbreviation (PST instead of PDT - See this) and convert the string, use to_timestamp_tz.
select to_timestamp_tz(PublishDate, 'MON-DD-YY HH24:MI:SS TZR')
from Orders;
or perhaps, convert them in one local time zone. See this answer for that
You can ignore the timezone by using:
SELECT TO_DATE(SUBSTR(PublishDate, 1, length(PublishDate) - 4), 'MON-DD-YY HH24:MI:SS')
However, if you want to handle different timezones, you need to store the standard format, something like 'America/Los_Angeles'.
For example:
SELECT TO_TIMESTAMP_TZ(PublishDate, 'MON-DD-YY HH24:MI:SS TZR')
from (select 'Sep-29-07 13:45:00 America/Los_Angeles' as PublishDate from dual) x;
You can this modify this with 'PDT', but that is not acceptable by itself.
I'm trying to convert the following String to a datetime format:
Sat Jan 14 13:55:34 CET 2017
If we consider that my date is in the in_OutPut3 variable, I'm trying to use the following format pattern :
TO_DATE(in_OutPut3,'DY MON DD HH:MI:SS Z YYYY')
I receive the following error :
The system failed to parse the date format : 'DY MON DD HH:MI:SS Z
YYYY'.
Do you have any idea what is this date format ?
You have a timezone embedded in your string so try using the TO_TIMESTAMP_TZ function. TO_DATE is not designed to work with timezones.
The following works on 11g;
select to_timestamp_tz('Sat Jan 14 13:55:34 CET 2017','DY MON DD HH24:MI:SS TZR YYYY') from dual
This one worked for me:
to_timestamp('Sat Jan 14 13:55:34 CET 2017','Dy Mon dd hh:mi:ss ZZ yyyy');
Marco
You can just ignore the timezone while converting to Informatica Date type. Anyway, Informatica won't retain timezone information after converting to Data datatype. Put some symbols like underscore(_) to match 'CET'
TO_DATE(in_OutPut3, 'DY MON DD HH24:MI:SS ___ YYYY')
Also, looks like you have the hours in military format. In that case you have to use HH24.