I have to deal with the following columns:
StartTime (nvarchar(10), null)
StartDate (nvarchar(10), null)
StartTime contains the Time as following: hhmmss
StartDate contains the Date as following: yyyymmdd (m=month)
To concatenate and convert StartDate
and StartTime
to a datetime
i use the following code inside a stored procedure:
TRY_CAST(SUBSTRING(xy.StartDate,7,2) + '.' + SUBSTRING(xy.StartDate,5,2) + '.' + SUBSTRING(xy.StartDate,1,4) + ' ' + SUBSTRING(xy.StartTime,1,2) + ':' + SUBSTRING(xy.StartTime,3,2) + ':' + SUBSTRING(xy.StartTime,5,2) as datetime) as FULLSTARTDATE
Now imagine these values for two cases:
Case 1:
StartTime = 000000
StartDate = 20191201
FULLSTARTDATE = 2019-01-12 00:00:00.000
Case 2:
StartTime = 000000
StartDate = 20191220
FULLSTARTDATE = NULL
Why does Case 2 not output 2019-20-12
? It may be a trivial issue but i can't seem to figure it out for almost an hour now.