whats wrong in this datetime_format ACCUM @@T_Date += datetime_format(to_datetime(R.Tdate,"%Y-%M-%D");
You have your parentheses wrong , see my other post
Cross linking these two threads