I have the following function "TicksToDateTime"
CREATE FUNCTION [dbo].[TicksToDateTime] (@t bigint)
RETURNS datetime
WITH SCHEMABINDING
AS
-- converts the given datetime to .NET-compatible ticks
-- see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclasstickstopic.asp
BEGIN
declare @result datetime
if (@t=0)
set @result = null
else
if (@t < 552877919999983334)
set @result = cast ('1753-1-1' as datetime)
else
if (@t=3155378975999999999)
set @result = cast ('9999-12-1' as datetime)
else
set @result = CAST((@t - 599266080000000000) / 10000000 / 24 / 60 / 60 AS datetime)
return @result
END
GO
and the use it in a computed column of a table:
[CallDateRaw] BIGINT NOT NULL,
[CallDate] AS ([dbo].[TicksToDateTime]([CallDateRaw])),
I am now trying to index the "CallDate" column like so:
Create Index ExternalCalls_CallDate2 ON [External.Call] (CallDate)
GO
But the index fails because the column is "non-deterministic" and I can confirm that the function is also non-deterministic with:
select object_id('tickstodatetime')
select OBJECTPROPERTYEX(2127346643, 'IsDeterministic')
Which returns false..
So my question is why is this function "non-deterministic" and how do I make it deterministic? from what I read on the internet it just said add "With SchemaBinding" but as you can see I've added that and it still doesnt work.
What am I doing wrong?