I have an XML column in a table; I want to ‘promote’ a certain value in that XML as a computed column and index it for faster searching. I have a function that takes in the XML information and outputs the element of interest, like this:
CREATE FUNCTION [dbo].[fComputeValue] (@data XML) RETURNS datetime WITH SCHEMABINDING AS BEGIN RETURN @data.value('(/Metadata/Value[@Key='StartDate'])[1]', 'datetime') END
However when I try to create the computed column:
ALTER TABLE dbo.CustomMetadataTable ADD [StartDate] AS ([dbo].[fComputeValue]([CustomMetadataColumn])) PERSISTED
I get the following error:
Msg 4936, Level 16, State 1, Line 2 Computed column ‘StartDate’ in table ‘CustomMetadataTable’ cannot be persisted because the column is non-deterministic.
It works if I:
- work with varchar, int, double (i.e. other than datetime) values
- remove the PERSISTED keyword (but then I can’t create an index on the column)
I should also mention that datetime values are in XSD datetime format. Any ideas? Thanks.
What about:
and:
or:
From books online:
It might help if you use
CONVERTwith style127