EDIT: Check out the solutions by Oleg and Gordon. I actually prefer them both to my own (as it's quite convoluted).
You need to get your date format from this...
'Wed, 19 Jul 2017 16:23:38 +0000'
...to this...
'19 Jul 2017 16:23:38'
You can remove chars from the beginning and end using LEFT
and RIGHT
. Removing the last 6 from the end would look like this:
LEFT([date], LEN[date] - 6)
We can use the same syntax for our RIGHT()
to remove the first 5, but [date]
must now be replaced with the entire string from above:
-- RIGHT([date], LEN([date]) - 5) becomes...
RIGHT(LEFT(@d, LEN(@d) - 6), LEN(LEFT(@d, LEN(@d) - 6)) - 5)
All in all, it's ugly, but works:
INSERT INTO feed.article(title,link,sourceID,[date])
SELECT title,link,s.sourceID,
CONVERT(DATETIME,RIGHT(LEFT([date], LEN([date]) - 6), LEN(LEFT([date], LEN([date]) - 6)) - 5),121)
FROM feed.tempXML t
JOIN feed.[source] s ON s.sourceName = t.[source]
IMPORTANT NOTE: This is under the assumption that the format of your date will always have 5 unnecessary characters at the beginning, and that your timezone offset (the +0000
at the end) will always be 0
(so we can simply ignore it).
If you'll have values that make use of the timezone offset, you'll need to account for that.