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.
Related
I'm using RSS feeds from multiple sources and I'm sorting the feeds by date. However, the dates I receive are in different time zones.
This is the format of some of the date strings that I get:
Wed 08 Dec 2021 01:40:31 -0500
Wed 08 Dec 2021 11:11:19 -0600
The "-500' indicates the time zone which is UTC-5. I need to use that "-0500" to convert this string into a date and the date needs to be only in UTC.
Basically from "Wed 08 Dec 2021 01:40:31 -0500" to "12/08/2021 06:40:31"
I've managed to split the string up to sort by date but because of the time difference, it doesn't really help.
Is there coding which I can use to convert the string as-is into date and only UTC time? Or is there just a place where I can start?
Thank you in advance.
Using DateTime.ParseExact, you specify a format to convert from, and that can include "zzz" for the timezone offset. You can also specify that you want the result in UTC:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTime.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing, Globalization.DateTimeStyles.AdjustToUniversal)
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"))
Console.WriteLine(d.Kind.ToString())
Outputs:
2021-12-08 06:40:31
Utc
Of course, format the result as you desire.
Alternatively, if you need to, you can keep the offset by using a DateTimeOffset structure and adjust it to UTC for representation as a string:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTimeOffset.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing)
Console.WriteLine(d.ToUniversalTime.ToString("yyyy-MM-dd HH:mm:ss"))
Outputs:
2021-12-08 06:40:31
I want to convert "Fri Nov 23 2018" into a mm/dd/yyyy format in Oracle 12c, I tried but didn'tfind a proper solution, kindly help me on this
Convert your string to a DATE data type (note, a DATE is a binary data-type and has no defined format so it is just being used as an intermediate step) and then back to a string in the desired format:
SELECT TO_CHAR(
TO_DATE(
'Fri Nov 23 2018',
'Dy Mon DD YYYY',
'NLS_DATE_LANGUAGE=American'
),
'mm/dd/yyyy'
) AS formatted_date_string
FROM DUAL;
Which outputs:
FORMATTED_DATE_STRING
11/23/2018
db<>fiddle here
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 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.
Please I need help on how to convert this string [ Fri Jun 19 10:45:39 EDT 2009
] that is in EDT date format back to Date in SQL (Am using Postgres). I want to be able to have something like this 19-06-2009
Thanks
SELECT
CAST('Fri Jun 19 10:45:39 EDT 2009' AS date), -- to get datatype DATE
TO_CHAR(CAST('Fri Jun 19 10:45:39 EDT 2009' AS date), 'DD-MM-YYYY') -- formated date, a string