Reputation: 1054
I have 2 date fields of dateTime data type each.
One is called PhaseEnd and the other is PhaseStart.
We would like to substract PhaseStart from PhaseEnd to get the number of months between the two.
When I run the following query:
SELECT (CASE WHEN PhaseEnd IS NOT NULL THEN round((PhaseEnd - PhaseStart)/30,1)
ELSE round((getdate() - PhaseStart)/30,1) END) Months from tblT_PHASES
I get Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query.
Any idea how to fix this?
Upvotes: 0
Views: 12137
Reputation: 117540
use datediff
select
case
when PhaseEnd is not null then
round(datediff(day, PhaseStart, PhaseEnd)/30,1)
else
round(datediff(day, PhaseStart, getdate())/30,1)
end as Months
from tblT_PHASES
or
select
round(
datediff(day, PhaseStart, isnull(PhaseEnd, getdate())) / 30
, 1) as Months
from tblT_PHASES
Upvotes: 1