Operand type clash: int is incompatible with date

  • Hello, i have been trying to solve this for sometime but i cant get around it. hopefully somebody can help me out here.

    this is a piece of my code:

    SELECT TOP 1000 *...

    case when Rownum<= datediff(day, salesdate, baseenddate)

    then DATEADD(mm, RowNum, salesdate) /*error at this point*/

    else 0 end as subscriptionrowdate

    FROM Subsrow

    Rownum is an integer type. DATEADD is the part when the error is but i dont know how to convert this to int ๐Ÿ™

    Any suggestions?

  • I got it working!

    DATEADD(mm, RowNum, convert(VARCHAR(10),salesdate,112))

  • mandania (7/3/2015)


    Hello, i have been trying to solve this for sometime but i cant get around it. hopefully somebody can help me out here.

    this is a piece of my code:

    SELECT TOP 1000 *...

    case when Rownum<= datediff(day, salesdate, baseenddate)

    then DATEADD(mm, RowNum, salesdate) /*error at this point*/

    else 0 end as subscriptionrowdate

    FROM Subsrow

    Rownum is an integer type. DATEADD is the part when the error is but i dont know how to convert this to int ๐Ÿ™

    Any suggestions?

    What datatype are salesdate and baseenddate?

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • they are both date type

  • mandania (7/3/2015)


    they are both date type

    Here's your error:

    case

    when Rownum<= datediff(day, salesdate, baseenddate) then DATEADD(mm, RowNum, salesdate)

    else 0 end

    You have two different datatypes returned by your CASE. Since date datatypes (returned by DATEADD) have a higher datatype precedence than INT, the ELSE condition will be changed to this:

    else CAST(0 AS <<a date datatype>>) end

    - which will fail with a DATE datatype but work with a DATETIME datatype.

    The return datatype of DATEADD is the same as the date parameter passed in. If it's a DATE, as you confirm, then you get the error. When you pass a string into DATEADD the return datatype is DATETIME, and you don't get an error.

    The solution?

    Change ELSE 0 END

    to ELSE <<a valid date of your choice, or NULL>> END.

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply