t sql – Convert and subtract two dates

Assuming that you have a valid reason to convert the date into a string and that you can not change that logic: declare a variable to store the intermediate results of the arithmetic of the date before converting it into a string, then you can easily obtain the Difference between the two dates:

DECLARE @eom DATE
= DATEADD (m, DATEDIFF (m, 0, DATEADD (m, 1, DATEADD (MM, -1, @maxDwdate))), -1);
DECLARE @EndOfMonth LIKE VARCHAR (10)
SET @EndOfMonth = (SELECT CONVERT (VARCHAR, @eom, 112))
DECLARE @diff INT;
SELECT @diff = DATEDIFF (DAY, @eom, DUTEDATE) AS DAYS
FROM t; - Replace with the real name of the table.