-6

want to write a check constraint(while creating a table) which accepts value between 2 dates like ('25-oct-94' to '10-may-16')

Rahul
  • 76,197
  • 13
  • 71
  • 125

2 Answers2

1

Since you are using Oracle you can use CHECK constraint saying

CONSTRAINT check_dates
  CHECK (my_date_column BETWEEN date '1994-10-25' AND date '2016-05-10')

Your query (as in comment) should be like below

 create table dob5 ( birthdate date not null, 
                    CONSTRAINT check_dates 
                    CHECK (birthdate BETWEEN date '1994-10-25' AND date '2016-05-10') );

See this demo fiddle http://sqlfiddle.com/#!4/779f9

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • it gives error @Rahul :- "date or system variable wrongly specified in CHECK constraint " create table dob5 ( birthdate date, CONSTRAINT check_dates CHECK (birthdate BETWEEN '25-oct-94' AND '10-may-16') ) this is how i tried – Prashant Tendulkar Jul 23 '17 at 15:30
  • ? are you there ? – Prashant Tendulkar Jul 23 '17 at 15:52
  • @PrashantTendulkar, see edit in answer if helps – Rahul Jul 23 '17 at 16:03
  • @PrashantTendulkar - you got that error because you did not create the constraint exactly as Rahul showed. You missed the `date` keyword, which is crucial when declaring date literals. – APC Jul 23 '17 at 16:40
0

please check this.

create table t

( your_columnnm date

check( your_columnnm between date '1994-10-25' and date '2016-05-10' ));

Ganesh
  • 486
  • 3
  • 8
  • 18