Search code examples
sql-serverunique-index

Unique filtered index with multiple conditions in SQL Server


Is it possible to create a filtered index in SQL Server with multiple conditions?

Here is what I am trying to do, but gives 'incorrect syntax' error:

CREATE UNIQUE NONCLUSTERED INDEX IX_TestTable  
ON TestTable(MyIntColumn)  
WHERE MyIntColumn is not null OR MyIntColumn<>0

Solution

  • Use the following syntax:

    CREATE UNIQUE NONCLUSTERED INDEX IX_TestTable  
    ON TestTable(MyIntColumn)  
    WHERE ISNULL(MyIntColumn,0) <> 0