When dealing with non-standard date formats, I usually lookup CONVERT
function in SQL Server, as it io very "elastic": you can provide various formats to it, so it recognizes them appropriately.
I use this site for reference. There you can find, that the closest date format is indicated by 100:
0 100 mon dd yyyy hh:miAM/PM Default
It's your format except it doesn't include unnecessary in this case day of week information. So all you need to do is cut out the Fri
part:
substring(myDatetimestring, 5, 1000)
or even better
substring(myDatetimeString, charindex(' ', myDatetimeString), 1000)
if the day of week part may have variable length. Don't worry about 1000
, it only assures that we will have the rest of the string, not any less.
Now we have proper date format, since it says that it's default, we don't need to supply 100
to convert
function and also we can use cast
function instead, as you discovered, so we could do:
cast(substring(myDatetimeString, charindex(' ', myDatetimeString), 1000) as datetime)
or
convert(datetime, substring(myDatetimeString, charindex(' ', myDatetimeString), 1000), 100)
or
convert(datetime, substring(myDatetimeString, charindex(' ', myDatetimeString), 1000))