Home Forums SQL Server 2005 T-SQL (SS2K5) Conversion failed when converting datetime from character string RE: Conversion failed when converting datetime from character string

  • You're setting @Month = '20100801'

    Then, in you're doing:

    BASE.SERVICE_YR_MO BETWEEN CONVERT(CHAR(6),DATEADD(M,-24,CONVERT(datetime,@MONTH + '01',112)),112) AND @MONTH

    AND

    BASE.PAID_YR_MO BETWEEN CONVERT(CHAR(6),DATEADD(M,-15,CONVERT(datetime,@MONTH + '01',112)),112) AND @MONTH

    Adding the string '01' to '20100801' gives you '2010080101', which is not a valid date.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2