• I thought it might be something to do with the version of SQL Server I am using (2000) so tried it on 2008R2 and get the same result.

    I'm not too familiar with debug having run it on my 2008R2 server the code runs OK but fails to apply the correct value to the "dd" or indeed "d" part of the @Datetime variable.

    Thanks.