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
 General SQL Server Forums
 New to SQL Server Programming
 A datetime constraint

Author  Topic 

Studevs
Starting Member

15 Posts

Posted - 2009-06-12 : 05:57:40
Dear all,

I have to a table with a datetime colum of the 'datetime' type.
I have to make a constraint for this column:
the default value of the column must be de date of today or older.
How can i do this?

Thanks.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-12 : 05:59:20
If you are not concerned about time part,
CHECK (DATEDIFF(DAY, MyDateColumn, GETDATE()) <= 0)

Otherwise as simple

CHECK (MyDateColumn <= GETDATE())

will do


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-12 : 06:02:00
CHECK datetime <=getdate()


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2009-06-12 : 06:18:52
Thanks a lot for your replay. it works fine.
however i have a another question:

im using sql server 2005.
in the designer when i click on my datetime column, i can set the 'default value or binding' for this column, or i can do the same through 'Constraints' in the table.
What's the difference between this to methodes?
quote:
Originally posted by senthil_nagore

CHECK datetime <=getdate()


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-12 : 06:23:00
Default value is what is inserted to a new record if you or an application doesn't supply the value.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Studevs
Starting Member

15 Posts

Posted - 2009-06-12 : 06:36:24
Thank you, that's clear now
quote:
Originally posted by Peso

Default value is what is inserted to a new record if you or an application doesn't supply the value.


E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page
   

- Advertisement -