Попробуйте это:
DECLARE @date VARCHAR(20)
SET @date ='3122019'
IF(LEN(@date) = 8)
BEGIN
SET @date = Stuff(Stuff(@date,5,0,'.'),3,0,'.');
SELECT CONVERT(DATE, @date , 103);
END
ELSE IF(LEN(@date) = 7)
BEGIN
SET @date = Stuff(Stuff(@date,4,0,'.'),2,0,'.');
IF(ISDATE(@date)=1)
BEGIN
SELECT CONVERT(DATE, @date , 103);
END
ELSE
BEGIN
SET @date = Stuff(Stuff(@date,4,0,'.'),3,0,'.');
SELECT CONVERT(DATE, @date , 103);
END
END
ELSE IF(LEN(@date) = 6)
BEGIN
SET @date = Stuff(Stuff(@date,3,0,'.'),2,0,'.');
SELECT CONVERT(DATE, @date , 103);
END