Ok I see the problem. In the code you function lost the single quotes around the 0.
IF (CHARINDEX ('DD',@DateValue) > 0)
SET @DateValue = REPLACE(@DateValue, 'DD',
RIGHT(0+DATENAME(DD, @Datetime),2))
If you place single quotes around the 0 that should do the trick.
IF (CHARINDEX ('DD',@DateValue) > 0)
SET @DateValue = REPLACE(@DateValue, 'DD',
RIGHT('0'+DATENAME(DD, @Datetime),2))