user3087472
user3087472

Reputation: 83

Unique attribute only when not null

I want to define an unique column, but only when it's not null.

What I mean is there can be multiple null values, but when they're not null they must be different. How can I do this?

Upvotes: 1

Views: 238

Answers (1)

juergen d
juergen d

Reputation: 204746

In SQL Server 2008 and above you can specify a condtion on your index

CREATE UNIQUE NONCLUSTERED INDEX IX_Filtered_Index_Name ON your_table (some_column)
WHERE some_column IS NOT NULL

Upvotes: 4

Related Questions