Declare @date1 varchar(8) = '872019',@date2 varchar(8) = '08122019'
Select
@date1 as date1,
CASE
WHEN LEN(@date1) < 8
THEN CAST(SUBSTRING(@date1,3,4)
+ REPLICATE('0',2-LEN(SUBSTRING(@date1,1,1))) + SUBSTRING(@date1,1,1)
+ REPLICATE('0',2-LEN(SUBSTRING(@date1,2,1))) + SUBSTRING(@date1,2,1) AS DATE)
ELSE CAST(SUBSTRING(@date1,5,4)+SUBSTRING(@date1,1,2)+SUBSTRING(@date1,3,2) as date) END as casted_date1,
@date2 as date2,
cast(substring(@date2,5,4)+substring(@date2,1,2)+SUBSTRING(@date2,3,2) as date) as casted_date2