June 5, 2020 at 9:36 pm
Can anyone help me out here. I need to convert strings in a file I received to datetime. An example format is: Wed Jun 29 15:57:45 2019
June 5, 2020 at 11:26 pm
if format is always the same you will need to use a bit of substrings and concatenation
extract following blocks
then concatenate year-month-day time with between day and time (and "-" on the others)
then use convert to datetime with format 120 (https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15#date-and-time-styles)
June 6, 2020 at 2:36 am
DECLARE @DateString nvarchar(100)='Wed Jun 29 15:57:45 2019'
SELECT CONVERT(datetime,SUBSTRING(@DateString,5,100))
June 6, 2020 at 2:08 pm
DECLARE @DateString nvarchar(100)='Wed Jun 29 15:57:45 2019'
SELECT CONVERT(datetime,SUBSTRING(@DateString,5,100))
I've always been astounded by the implicit conversions of strings to DateTime that SQL Server handles correctly. It seriously outstrips the formatting available through CONVERT and people seem to thing that's all it can handle for string conversions.
I have to admit that I've not seen this format before but, like you, I would have tried it anyway and would have, once again, been astounded by its implicit conversion capabilities.
Nicely done, Jonathan.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 10, 2020 at 9:41 am
This was removed by the editor as SPAM
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply