Search code examples
sql-serversql-server-2005indexed-view

SQL Server ISDATE In Indexed View


I have a indexed view where I basically need to do this

SELECT ... 
    CASE 
         WHEN ISDATE(ColumnName) = 1 THEN CONVERT(datetime, ColumnName, 103) 
         ELSE NULL 
    END AS ViewColumn
....

Trying to create the index yields:

Cannot create index on view '....'. The function 'isdate' yields nondeterministic results. Use a deterministic system function, or modify the user-defined function to return deterministic results.

MSDN says

ISDATE is deterministic only if you use it with the CONVERT function,
if the CONVERT style parameter is specified, and style is not equal to 0, 100, 9, or 109.

here http://msdn.microsoft.com/en-us/library/ms187347.aspx.

But I don't know what that means at all. As far as I can tell, I am using it with a CONVERT function....

Any way to work around this?


Solution

  • It should be, if at all:

    SELECT ... 
        CASE 
             WHEN ISDATE(ColumnName) = 1 THEN CONVERT(datetime, ColumnName, 103) 
             ELSE NULL 
        END
    ....
    

    but, you are not using ISDATE WITH CONVERT, since there is no expression like

    ISDATE(CONVERT(varchar,ColumnName,112)) 
    

    without the nested convert the return value is dependend on things like language settings, hence it's nondeterministic behaviour. Without "external" knowledge, it's not possible to predict the result one is getting, based on the input alone.