Reputation: 862
on SQL Server 2012 a have Varchar(200) containing values like (JS .toString() date):
Thu Jul 05 2018 20:43:14 GMT+0200
is there an easy way to convert the varchar to datetime? I tried with the
convert
command but I couldn't find any date style.
Upvotes: 1
Views: 244
Reputation: 182
If you can change the JavaScript use toISOString
instead of toString
.
Then on Sql Server:
convert(datetime, @YourVarChar, 127)
If you can't then try this:
DECLARE @myDate as varchar(35) = 'Thu Jul 05 2018 20:43:14 GMT+0200';
Declare @myMonth as char(3),
@myDay as char(2),
@myYear as char(4),
@myTime as char(8),
@parseMe as varchar(24),
@newDateTime as datetime;
set @myMonth = SUBSTRING(@myDate,5,3);
set @myDay = SUBSTRING(@myDate,9,2);
set @myYear = SUBSTRING(@myDate,12,4);
set @myTime = SUBSTRING(@myDate,17,8);
set @parseMe = @myDay + ' ' + @myMonth + ' ' + @myYear + ' ' + @myTime + ':000'
set @newDateTime = convert(datetime,@parseMe,113)
A little dirty, but works. Also doesn't take timezone into consideration.
Upvotes: 1