How to convert this piece of code from Oracle to get the same behaviour in SQL Server ?
select to_date('20141008174025', 'YYYYMMDDHH24MISS')
from dual
How to convert this piece of code from Oracle to get the same behaviour in SQL Server ?
select to_date('20141008174025', 'YYYYMMDDHH24MISS')
from dual
One method uses datetimefromparts()
:
declare @str varchar(20) = '20141008174025';
select datetimefromparts(
substring(@str, 1, 4),
substring(@str, 5, 2),
substring(@str, 7, 2),
substring(@str, 9, 2),
substring(@str, 11, 2),
substring(@str, 13, 2),
0
)
You can also do:
declare @str varchar(20) = '20141008174025';
select cast(
left(@str, 8)
+ ' '
+ substring(@str, 9, 2)
+ ':'
+ substring(@str, 11, 2)
+ ':' + substring(@str, 13, 2)
as datetime
)