The "Deterministic" rules are pretty nasty when it comes to CONVERT and implicit converts. That's one of the many reasons why dates represented as integers are so frowned upon. Here are the rules according to Books Online.
The following functions are not always deterministic, but can be used in indexed views or indexes on computed columns when they are specified in a deterministic manner.
Function | Comments |
---|---|
CAST | Deterministic unless used with datetime, smalldatetime, or sql_variant. |
CONVERT | Deterministic unless one of these conditions exists:
|
Here's your PERSISTED computed column using the intended formula (365/12 was incorrect... it needed to be 365/12.0 or 30.416666 to meet the requirements according to the ROUND used in the original formula). I'm not sure that I agree with that formula but that's up to you. The requirement to return the number of months as an INT required yet another CONVERT.
CREATE TABLE dbo.FctCoverageLengthDetail
(
CoverageEffectiveDateKey INT
,CoverageEndDateKey INT
,CalculatedMonths AS CONVERT(INT,ROUND(
DATEDIFF(dd
,CONVERT(DATETIME,CONVERT(CHAR(8),CoverageEffectiveDateKey),112)
,CONVERT(DATETIME,CONVERT(CHAR(8),CoverageEndDateKey) ,112)
)/30.416666 --(365/12.0)
,0)) PERSISTED
)
;
--Jeff Moden
Change is inevitable... Change for the better is not.