You can use
LEFT()
and
RIGHT()
to do so.
Example:
declare @test varchar(30)
set @test='1160119104041'
select '20'+left(right(@test,LEN(@test)-1),2)+'-'+left(right(@test,LEN(@test)-3),2)+'-'+left(right(@test,LEN(@test)-5),2)+' '+left(right(@test,LEN(@test)-7),2)+':'+left(right(@test,LEN(@test)-9),2)+':'+left(right(@test,LEN(@test)-11),2)
You may want to cast it to
DATETIME
using
CAST(expressio AS DATETIME)
.
Note: this is just a way. You may still look for better options or suggestions.
You may also need to think, what if year is 98 and you want it to be 1998!
Hope, it helps :)
Update
You can create a function like -
CREATE FUNCTION GetDateFromVarchar
(
@InputValue VARCHAR(30)
)
RETURNS DATETIME
AS
BEGIN
DECLARE @ReturnValue VARCHAR(30)
SELECT @ReturnValue= '20'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-1),2)+'-'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-3),2)+'-'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-5),2)+' '+LEFT(RIGHT(@InputValue,LEN(@InputValue)-7),2)+':'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-9),2)+':'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-11),2)
RETURN CAST(@ReturnValue AS DATETIME)
END
GO
Note: It not advised to copy paste code. Check and modify according to your need. I haven't tested this yet but should work.