I am receiving a datetime in the following format:
Thu, 18 Mar 2021 10:37:31 +0000
If I'm correct this if RFC2822.
I can't figure out a way to convert this to a 'normal' datetime that would be used by SQl server.
For example I want it to be:
2021-03-18 10:37:31
YYYY-MM-DD hh:mi:ss
I have tried things like CONVERT() and found a sketchy way by doing:
DECLARE #WeirdDate varchar(50) = 'Thu, 30 Jul 2015 20:00:00 +0000'
SELECT
CONVERT(DATETIME, SUBSTRING(#WeirdDate, CHARINDEX(',', #WeirdDate) + 1, 20))
But none of it is working that well.
Is there a way to convert this in a 'proper' way?
edit:
To clarify:
The format should always be the same as the provided example. Including the day name.
I am not sure that it will always be the same timezone. I could be receiving it from a different timezone. This is something to consider.
You could achieve this with a "little" string manipulation is seems, and some style codes:
DECLARE #YourDate varchar(50) = 'Thu, 30 Jul 2015 20:00:00 +0000'
SELECT TRY_CONVERT(datetimeoffset(0),CONVERT(varchar(25),TRY_CONVERT(datetime2(0),STUFF(STUFF(#YourDate,1,5,''),21,6,''),106),126) + STUFF(RIGHT(#YourDate,5),4,0,':'));
This will, however, fail if you're using a LOGIN with a language setting which isn't an English based one.
If the value is always UTC, you can actually just use the "middle" TRY_CONVERT expression.
In my case the string comes with extra information
Mon Feb 15 2021 15:48:09 GMT-0500 (Colombia Standard Time)
Sun Mar 28 2021 09:59:51 GMT-0500 (hora de Ecuador)
Fri Jun 25 2021 08:54:28 GMT-0500 (Ecuador Time)
I used some replace functions to get rid of the extra information
TRY_PARSE( replace( replace( replace( replace( <<date_field>>, '(Colombia Standard Time)', '' ), 'GMT', '' ), '(hora de Ecuador)', ''), '(Ecuador Time)', '' ) as datetime using 'en-US')
Related
So I have a table and I am trying to convert the time from its current format that looks like this
Sep 17 2020 1:07AM
to
2020-09-16 20:07:00.000
if I try to update the entire table by
UPDATE
OOTYPE
SET
OOTYPE.LastModified = CONVERT(DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, LastModified),
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
it runs successfully but the times are still in the format
Sep 17 2020 1:07AM
However, if I just run a simple select statement to show it changed
SELECT CONVERT(DATETIME, SWITCHOFFSET(CONVERT(DATETIMEOFFSET,lastmodified), DATENAME(TzOffset, SYSDATETIMEOFFSET()))) FROM [OOTYPE]
The output is exactly what I'm looking for.....
2020-09-16 20:07:00.000
Anyone have any idea where I'm screwing this simple change up at, I have put an embarrassing amount of time into this....
If I follow you correctly:
SELECT CONVERT(varchar, CONVERT(datetime, 'Sep 17 2020 1:07AM'), 121)
You should use convert style 121 which is yyyy-mm-dd hh:mi:ss.mmm.
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.
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
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 :)
How can I convert the string 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', stored as a varchar(max), to the datetime value '09-03-2012', which is the date part of the string?
Use convert and reference to this page http://msdn.microsoft.com/en-us/library/ms187928.aspx
e.g.
CONVERT(varchar(8), GETDATE(), 112)
According to MS: http://msdn.microsoft.com/en-us/library/ms187928.aspx
The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
and the table lists 105 as the code for the style you want.
So, for your example date:
CONVERT(VARCHAR(10), 'Fri, 9 Mar 2012 10:43:21 +0000 (UTC)', 105)
should probably do the trick.
I had to chop up your date first, but here goes:
DECLARE #ds varchar(30)='Fri, 9 Mar 2012 10:43:21 +0000 (UTC)';
SELECT CONVERT(VARCHAR(10)
,CAST(SUBSTRING(#ds
,CHARINDEX(' ',#ds)
,LEN(#ds)-(PATINDEX('%[0-9][0-9][0-9][0-9]%',#ds)+6))
AS DATETIME)
,105
)
Result:
09-03-2012
If you have SQL Server 2012, you can try this:
select CONVERT(date, TRY_PARSE('Fri, 9 Mar 2012 10:43:21 +0000' AS datetime2))
NOTE: (UTC) is removed, it didn't parse correctly.
Also, be aware if you need to localize that time value. For example, I live in AZ, so typically I just subtract -7 hrs from UTC times to localize. You will need to make similar adjustments based on the time zone