Houston, we have a problem with 20070907030987
if OBJECT_ID('tempdb..#dstyles') is not null
drop table #dstyles
go
create table #dstyles(funkydates varchar(150))
insert into #dstyles
select '20090225121337' union
select '20030413082345' union
select '20070907030987'
select DATETIMEFROMPARTS(_year, _month,_day,_hour,_minute,_second, _millivanilly)
from (
select SUBSTRING(funkydates, 1, 4) as _year,
SUBSTRING(funkydates, 5, 2) as _month,
SUBSTRING(funkydates, 7, 2) as _day,
SUBSTRING(funkydates, 9, 2) as _hour,
SUBSTRING(funkydates, 11, 2) as _minute,
SUBSTRING(funkydates, 13, 2) as _second,
0 _millivanilly
from #dstyles
) _slimx
Select cast(stuff(stuff(stuff(funkydates, 13, 0, ':'), 11, 0, ':'), 9, 0, ' ') As datetime)
from #dstyles
This does not work on some of those dates, I think it is the odd hours part that starts with 0
Cannot construct data type datetime, some of the arguments have values which are not valid.