2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

3

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

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
3

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.

Community
  • 1
  • 1
Alex
  • 21,273
  • 10
  • 61
  • 73