Convert string into date in SQL Server - sql

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

Related

RFC2822 datetime format to a usable datetime in SQL server

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')

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.

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 :)

Need to identify number that is higher than a given date

I have a date column in a table. The date column is in varchar. I want to identify a particular date range from that date column. My query is like this:
SELECT *
FROM [003 AccptReg].[dbo].[SysData1]
WHERE [RegDate_Sys] > '18 jul 2013'
But the result is not giving accurate result, i.e. it gives dates which are prior of 18 jul 2013.
Is there any thing wrong I am doing?
For date column, you should compare as DATE
select * from [003 AccptReg].[dbo].[SysData1]
where CAST([RegDate_Sys] AS DATE) > CAST('18 jul 2013' AS DATE)
The problem is that you have the date as a varchar, and doesn't convert it to a date when you are doing the comparison. The database doesn't know that you see the data as dates, and will simply compare them as strings, so for example '2 jan 1736' will be larger than '18 jul 2013' because 2 comes after 1.
The best would be if you could store the data as datetime values (or date), then you don't need to do the conversion when you compare the values, which would give better performance.
If that's not possible, do the conversion in the query:
select * from [003 AccptReg].[dbo].[SysData1]
where convert(datetime, [RegDate_Sys], 106) > '18 jul 2013'
Depending on the settings on the server, you might also need to convert '18 jul 2013' in the same way for the database to understand it correctly as a date.
Convert the date to datetime format and then compare:
select * from [003 AccptReg].[dbo].[SysData1]
where convert(datetime,[RegDate_Sys]) >convert(datetime,'18 jul 2013')

How to extract date from datetime stored as a varchar

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