Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Check Constraint on Months

Author  Topic 

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-05-15 : 15:30:18
Hi

I have a table with 3 columns in it. The ID column has a datetime data type.

Does anyone know how to apply a check constraint on this field so that the same month cannot be entered twice.

For example the field has the following data in the field
26/04/2008
27/05/2008
26/06/2008
25/07/2008

A user then tries to enter the value 20/05/2008, I would like the check constraint to block this value being entered due to the fact that a item with the same month value has already been entered.

Is this possible?

Thanking you in Advance!!!

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-05-15 : 18:28:55
You can't do this with a check constraint, because they only look at columns within the same row, and you want to check for unique months between rows.

You would have to use a unique constraint on a column in the table or in a view.






CODO ERGO SUM
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-15 : 18:32:45
I suppose you could also use a trigger if you are unable to modify the schema. The trigger would check for the issue and rollback if found.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

rcr69er
Constraint Violating Yak Guru

327 Posts

Posted - 2008-05-16 : 03:27:35
HI

Thanks for that, I was thinking about using a trigger, but wasn't sure about how to do the process.
Go to Top of Page
   

- Advertisement -