TBSupport
asked on
SSRS: Turning a Date Value into an Integer for a Mathematical Calculation
Hello:
At the end of this posting is a snippet of code from my larger query. There are two fields, here: "Total" and "Month".
I want to leave Month the way that it is, but I need to change the Total field. Specifically, I need to multiply what I show now as Total, by an integer. This integer is based on a timeframe.
Specifically, the RCG.dbo.ContractHeader table has StartDate and EndDate fields. I need to subtract StartDate from EndDate and, then, subtract GetDate(). This should give me an integer representing number of months.
It's this number of months that I will multiply times the Total field's product of AvailQuantity and UNITCOST (from the code below). This will ultimately give me the value of the amount of time remaining in the contract!
Since StartDate and EndDate can represent months that cut across years, how do I derive a number of months and do what I just said I need: (EndDate-StartDate)-GetDat e()?
Thanks!
TBSupport
SELECT
(RCG.dbo.POContractDetail. AvailQuant ity)*(RCG. dbo.POCont ractDetail .UNITCOST) AS Total,
'Month ' + CASE WHEN DATEDIFF(m,GETDATE(),RCG.d bo.POContr actHeader. EndDate) + 1 < 10 THEN CAST(CAST('0' AS VARCHAR) + CAST(DATEDIFF(m,GETDATE(), RCG.dbo.PO ContractHe ader.EndDa te) + 1 AS VARCHAR(1)) AS VARCHAR(2))
ELSE CAST(DATEDIFF(m,GETDATE(), RCG.dbo.POContractHeader.E ndDate) + 1 AS VARCHAR(2)) END AS Month,
At the end of this posting is a snippet of code from my larger query. There are two fields, here: "Total" and "Month".
I want to leave Month the way that it is, but I need to change the Total field. Specifically, I need to multiply what I show now as Total, by an integer. This integer is based on a timeframe.
Specifically, the RCG.dbo.ContractHeader table has StartDate and EndDate fields. I need to subtract StartDate from EndDate and, then, subtract GetDate(). This should give me an integer representing number of months.
It's this number of months that I will multiply times the Total field's product of AvailQuantity and UNITCOST (from the code below). This will ultimately give me the value of the amount of time remaining in the contract!
Since StartDate and EndDate can represent months that cut across years, how do I derive a number of months and do what I just said I need: (EndDate-StartDate)-GetDat
Thanks!
TBSupport
SELECT
(RCG.dbo.POContractDetail.
'Month ' + CASE WHEN DATEDIFF(m,GETDATE(),RCG.d
ELSE CAST(DATEDIFF(m,GETDATE(),
ASKER
I got a negative figure for the total field and my data is duplicating.
Hi TB,
Did you put the ENDATE before the STARTDATE in your DATEDIFF statement? Assuming that your startdate preceeds your enddate, and your enddate is not in the future (as in an estimated enddate for a contract), then should only be able to produce a positive number if you put the earlier date ahead of the later date in the DATEDIFF statement. Can you copy what you ended up using?
Did you put the ENDATE before the STARTDATE in your DATEDIFF statement? Assuming that your startdate preceeds your enddate, and your enddate is not in the future (as in an estimated enddate for a contract), then should only be able to produce a positive number if you put the earlier date ahead of the later date in the DATEDIFF statement. Can you copy what you ended up using?
ASKER
The end date is, indeed, in the future. Again, that's why I said that the Start Date and End Date fields can cross into years and, specifically, future years.
ASKER
This is what I'm using. At your leisure, please help me get rid of the negatives.
For now, I have put this aside.
SELECT RCG.dbo.POContractHeader.V ENDORID as VendorID,
--(RCG.dbo.POContractDetai l.AvailQua ntity)*(RC G.dbo.POCo ntractDeta il.UNITCOS T) AS Total,
(DATEDIFF(M, RCG.dbo.POContractHeader.E ndDate, RCG.dbo.POContractHeader.S tartDate)) +(DATEDIFF (M, GETDATE(), RCG.dbo.POContractHeader.E ndDate))
*(RCG.dbo.POContractDetail .AvailQuan tity)*(RCG .dbo.POCon tractDetai l.UNITCOST ) AS Total,
'Month ' +
CASE WHEN DATEDIFF(m,GETDATE(),RCG.d bo.POContr actHeader. EndDate) + 1 < 10
THEN CAST(CAST('0' AS VARCHAR) + CAST(DATEDIFF(m,GETDATE(), RCG.dbo.PO ContractHe ader.EndDa te) + 1 AS VARCHAR(1)) AS VARCHAR(2))
ELSE CAST(DATEDIFF(m,GETDATE(), RCG.dbo.POContractHeader.E ndDate) + 1 AS VARCHAR(2))
END
AS Month, 0 as PastDue
FROM RCG.dbo.POContractHeader INNER JOIN
RCG.dbo.POContractDetail ON RCG.dbo.POContractHeader.C ontractNum =
RCG.dbo.POContractDetail.C ontractNum LEFT OUTER JOIN
RCG.dbo.PODraws ON RCG.dbo.POContractHeader.C ontractNum =
RCG.dbo.PODraws.ContractNu m
WHERE (RCG.dbo.POContractHeader. EndDate >= DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)) AND
(RCG.dbo.POContractHeader. EndDate < DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 18, 0))
--and RCG.dbo.POContractHeader.V ENDORID IN (@Vendor)
and RCG.dbo.POContractHeader.S tartDate IS NOT NULL and RCG.dbo.POContractHeader.E ndDate IS NOT NULL
GROUP BY RCG.dbo.POContractHeader.V ENDORID, RCG.dbo.PODraws.DateReq, RCG.dbo.POContractDetail.A vailQuanti ty, RCG.dbo.POContractDetail.U NITCOST, RCG.dbo.POContractHeader.S tartDate,
RCG.dbo.POContractHeader.E ndDate
HAVING (RCG.dbo.POContractDetail. AvailQuant ity)*(RCG. dbo.POCont ractDetail .UNITCOST) > 0
For now, I have put this aside.
SELECT RCG.dbo.POContractHeader.V
--(RCG.dbo.POContractDetai
(DATEDIFF(M, RCG.dbo.POContractHeader.E
*(RCG.dbo.POContractDetail
'Month ' +
CASE WHEN DATEDIFF(m,GETDATE(),RCG.d
THEN CAST(CAST('0' AS VARCHAR) + CAST(DATEDIFF(m,GETDATE(),
ELSE CAST(DATEDIFF(m,GETDATE(),
END
AS Month, 0 as PastDue
FROM RCG.dbo.POContractHeader INNER JOIN
RCG.dbo.POContractDetail ON RCG.dbo.POContractHeader.C
RCG.dbo.POContractDetail.C
RCG.dbo.PODraws ON RCG.dbo.POContractHeader.C
RCG.dbo.PODraws.ContractNu
WHERE (RCG.dbo.POContractHeader.
(RCG.dbo.POContractHeader.
--and RCG.dbo.POContractHeader.V
and RCG.dbo.POContractHeader.S
GROUP BY RCG.dbo.POContractHeader.V
RCG.dbo.POContractHeader.E
HAVING (RCG.dbo.POContractDetail.
Hi TB,
If I understand correctly, and your objective is to determine how many months remain in the contract. If that's the case then if startdate is in the past and enddate is in the future, we don't need to use startdate at all, we just want the difference between GETDATE() and the endate of the contract. In cases where contracts END in the past you probably want to see zero (0) -- assuming that's even possible in your data set. Lastly, if the startdate is also in the future, then naturally so is the enddate, and I assume you want to see an integer representing the total length of the contract (as opposed to the difference between the end of the contract and the current date..)
Assuming I'm understanding I would use logic like the following:
Please advise
If I understand correctly, and your objective is to determine how many months remain in the contract. If that's the case then if startdate is in the past and enddate is in the future, we don't need to use startdate at all, we just want the difference between GETDATE() and the endate of the contract. In cases where contracts END in the past you probably want to see zero (0) -- assuming that's even possible in your data set. Lastly, if the startdate is also in the future, then naturally so is the enddate, and I assume you want to see an integer representing the total length of the contract (as opposed to the difference between the end of the contract and the current date..)
Assuming I'm understanding I would use logic like the following:
CASE
WHEN [yourstartdate] > GETDATE()
THEN DATEDIFF(M, [yourstartdate], [yourenddate])
WHEN [yourenddate]<= GETDATE()
THEN 0
ELSE DATEDIFF(M, GETDATE(), [yourenddate])
END As 'Months_Remaining'
Please advise
ASKER
Hi CoreConcepts:
You're correct, per the first statement that you made: "my objective is to determine how many months remain in the contract. If that's the case then if startdate is in the past and enddate is in the future, we don't need to use startdate at all, we just want the difference between GETDATE() and the endate of the contract."
That being the case, does that change the code that you suggested, in your recent posting?
TBSupport
You're correct, per the first statement that you made: "my objective is to determine how many months remain in the contract. If that's the case then if startdate is in the past and enddate is in the future, we don't need to use startdate at all, we just want the difference between GETDATE() and the endate of the contract."
That being the case, does that change the code that you suggested, in your recent posting?
TBSupport
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it. Thank you!
TBSupport
TBSupport
It sounds like what would work is getting the difference between start and end date using DATEDIFF like:
DATEDIFF(M, <YourStartDate>, <YourEndDate>) -- if you reverse the order your number will be negative..
Then.. do the difference between your End Date and GETDATE() like:
DATEDIFF(M, <YourEndDate>, GETDATE())
Then Add the two together:
Open in new window
Then you could apply that in your CASE statement - if that isn't what you're looking for please advise.