Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check constraint for date

I need a check constraint for date so no data can be entered after today's/this date.

please advise thanks all

like image 664
martin Avatar asked Apr 24 '26 19:04

martin


2 Answers

Try this:

ALTER TABLE yourTable
ADD CONSTRAINT yourDateTimeColumn CHECK (yourDateTimeColumn < GetDate() );
like image 62
JTorrecilla Avatar answered Apr 27 '26 10:04

JTorrecilla


To remove the time part on SQL Server 2008, use cast...date

Also, I assume you can have dates including today

ALTER TABLE yourTable
ADD CONSTRAINT yourDateTimeColumn CHECK (
    CAST(yourDateTimeColumn as date) <= CAST(GETDATE() as date)
     );
like image 23
gbn Avatar answered Apr 27 '26 11:04

gbn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!