What I want to achieve: To create an index with an existing date column data converted into UTC timing (Query is written below)
Issue: There's a column in table with local server date values, I need to convert them into UTC date value and then create an Index over it.
Why I need this: I have the same thing done in oracle, and I am trying to migrate stuff along with queries into Sql Server for a new client
Problem: Index doesn't take variables or user defined functions. But only takes table columns as parameters.
Only Work around: is to make a computed column on table and use it to create the index.
Steps I followed:
Ran the below queries at first
ALTER TABLE dbo.tableClient ADD tempComp1 AS DATEADD(minute, datediff(minute, GETUTCDATE(), getdate()), [svrDate]) GO create index idx1 on dbo.tableClient([key] asc, [tempComp1] desc, [type]) GO
It gives the below error:
Column 'tempComp1' in table 'dbo.tableClient' cannot be used in an index or statistics or as a partition key because it is non-deterministic.
So I tried making the column as PERSISTED
ALTER TABLE dbo.tableClient ADD tempComp1 AS DATEADD(minute, datediff(minute, GETUTCDATE(), getdate()), [svrDate]) PERSISTED
it now giving the error:
Computed column 'tempComp1' in table 'tableClient' cannot be persisted because the column is non-deterministic.
Now, the funny thing is, if I do
SELECT datediff(minute, GETUTCDATE(), getdate())
it gives result: 330
Now if I try the same commands with 330
ALTER TABLE dbo.tableClient ADD tempComp1 AS DATEADD(minute, 330, [svrDate]) PERSISTED
GO
create index idx1 on dbo.tableClient([key] asc, [tempComp1] desc, [type])
GO
it works absolutely fine.