How to use only the year of a DateTime column in a SQL Unique Constrain?
sql, sql-server, unique-key
Solution
Use a computed column and then created the index. Something like:
alter table registry add RegistryYear as year(RegistryDate);
create index registry_number_year on Registry(number, RegistryYear);
Problem
Im creating a Unique constraint, that only allows to use the a Registry number per year, with this i mean that can exist more that one number 2, in the registry but only if this were created on different years. I have heard that it is possible to do ir with the Unique constraint but i do not know how, without needing to create a column for year and another for month, and ect. Query ALTER TABLE dbo.correspondencia_FFAA ADD CONSTRAINT uk_correspondecia UNIQUE (num_corres, fecha_cre); num_corres is the Registry Number, and fecha_cre is the Creation Date, but i only need the year not the whole column, is it possible Thanks