Converting String (in Eastern Daylight Time format) to Date in SQL - sql

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

Related

How to convert long verbal datetime to timestamp(YYYY-MM-DD HH:MM:SS) in SNOWFLAKE

In snowflake, I have a bad date format, something like this -
'Tue Aug 06 18:22:59 EDT 2019'
I am trying to convert it to the following date format -
YYYY-MM-DD HH:MM:SS
I have tried various TO_TIMESTAMP versions but nothing seems to work. Any suggestions/ help is truly appreciated.
SELECT TO_TIMESTAMP_NTZ('Wed May 29 23:36:39 EDT 2019', 'DY MON DD HH:MM:SS YYYY')
Error msg - Can't parse 'Wed May 29 23:36:39 EDT 2019' as timestamp with format'DY MON DD HH:MM:SS YYYY'
This works in my Snowflake environment:
SELECT to_timestamp('Wed May 29 23:36:39 EDT 2019', 'DY MON DD HH24:MI:SS EDT YYYY') as dt
Using MI for minutes (not MM).
Output: 2019-05-29 23:36:39.000

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.

How to convert the date format from one to another in sql server?

I have a column with the date format as Tue, 29 May 2012 16:04:05 EDT
I want to convert this format to the this format:29-05-2012
Thanks in advance
SELECT PARSE(REPLACE('Tue, 29 May 2012 16:04:05 EDT', 'EDT', '')AS datetime USING 'en-US')AS [Date&Time];
OR
SELECT dateadd(HH, +4, parse(substring('Tue, 29 May 2012 16:04:05 EDT', 1, 25) as datetime))
declare #EDTtext varchar(29),
#EDTdate date
-- key is remove abbreviation EDT
select #EDTtext = 'Tue, 29 May 2012 16:04:05 EDT'
select #EDTdate = parse(substring(#EDTtext, 1, 25) as date)
select format(#EDTdate, 'dd-MM-yyyy') -- format 29-05-2012
If you have different timezone than EDT don't forget to offset.

Convert string into date in SQL Server

Conversion failed when converting date and/or time from character string.
I'm getting the above error when running this statement in SQL Server:
SELECT CONVERT(datetime, 'Fri, 15 Jan 2016 17:30:05 GMT')
Actually I want to insert same string format in Datetime column
As suggested by Tim Biegeleisen, that string needs to be processed to be converted. In order to convert it you need to strip of the day (Fri,) and the GMT timezone at the end, for example:
DECLARE #date varchar(50) = 'Fri, 15 Jan 2016 17:30:05 GMT'
SELECT CONVERT(DATETIME, SUBSTRING(#date, 5, LEN(#date) - 8), 113)
This solution does strip the timezone information, have a look at this post if you want to convert it back to UTC.
If you want to insert the string 'Fri, 15 Jan 2016 17:30:05 GMT' into datetime column then you need to remove Fri, and GMT from the string before inserting.
SELECT CAST(substring(#str_date,5,len(#str_date)-8) as datetime)
DEMO

How to cast Date from varchar to DATETIME

Hey I have column called DATE_Hour in my table which is varchar and has values like
Tue, 29 Jul 2014 14:00
How can I convert that date in to 2014-07-29 14:00:00.000?
Thanks
If it consistently has the day of the week followed by a comma, you can use:
SELECT CAST(STUFF(DATE_Hour,1,CHARINDEX(',',DATE_Hour),'')AS DATETIME)
FROM YourTable
The format: '29 Jul 2014 14:00' will cast without issue, but the day of the week preceding that is not an accepted format, so you just use STUFF() to strip off the day then CAST() what remains.
DECLARE #str VARCHAR(100) = 'Tue, 29 Jul 2014 14:00'
PRINT CONVERT(VARCHAR(100),CAST(RIGHT(#str,LEN(#str)-CHARINDEX(',',#str)) AS DATETIME),121)
Try this, it will work no matter what you have in there for the day of the week (i.e. could be WED, THURS, etc. just in case your day of the week isn't always three characters):
DECLARE #VarcharTime VARCHAR(50) = 'Tue, 29 Jul 2014 14:00'
SELECT CAST(SUBSTRING(#VarcharTime, PATINDEX('%, %',#VarcharTime)+2, LEN(#VarcharTime)) AS DATETIME)
EDIT: This will also work with PATINDEX('%,%',#VarcharTime)+1 if you're interested in saving characters :)