Question : Restrict value to field - unique but allow certain values

Hi,

I need to add a rule to a field so that it allows only unique values except if the value is either '-' or 'not to be used', is this possible?

Thanks,

Sanj

Answer : Restrict value to field - unique but allow certain values

I would say you have to create new computed column which will be unique. You may calculate it as conditional expression based on some unique column from your table and your "pseudo unique" column. Let suppose the ID column (numeric) is a primary key in your table:

ALTER TABLE YourTable ADD NewUniqueCol AS YourColumn+CAST(CASE WHEN YourColumn IN ('-', 'not to be used') THEN CAST(LTRIM(STR(ID)) AS varchar(30)) ELSE '' END AS varchar(100))

And now you can create unique index on this computed column which does the work:

create unique index pseudo ON YourTable (NewUniqueCol)
Random Solutions  
 
programming4us programming4us