July 3, 2015 at 3:33 am
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?
July 3, 2015 at 3:44 am
I got it working!
DATEADD(mm, RowNum, convert(VARCHAR(10),salesdate,112))
July 3, 2015 at 6:33 am
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?
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
July 3, 2015 at 6:35 am
they are both date type
July 3, 2015 at 6:58 am
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.
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