I have this function for a computed column :
CREATE FUNCTION [dbo].[GetAllocatedStartTime](@Year INT, @Week INT)
RETURNS DATETIME
WITH schemabinding
AS BEGI
CONVERT([varchar](4),@Year,(0))+'-01-01'
is being passed to a DATEDIFF
call, in a position where a date is expected, forcing an implicit conversion to occur.
From the rules for deterministic functions:
CAST
Deterministic unless used with
datetime
,smalldatetime
, orsql_variant
.
CONVERT
Deterministic unless one of these conditions exists:
...
Source or target type is
datetime
orsmalldatetime
, the other source or target type is a character string, and a nondeterministic style is specified. To be deterministic, the style parameter must be a constant. Additionally, styles less than or equal to 100 are nondeterministic, except for styles 20 and 21. Styles greater than 100 are deterministic, except for styles 106, 107, 109 and 113.
Well, you're calling neither, but you're relying on an implicit conversion, which I'd expect to act like CAST
. Rather than rely on this, I'd switch to using CONVERT
and give a deterministic style parameter.
So, I'd do: CONVERT(datetime,CONVERT([varchar](4),@Year,(0))+'0101',112)
in its place. Having done so, the function itself becomes deterministic