I am facing issue adding constraint to the column of table in oracle- The column is defined with the datatype char(500 char). I need to put the constraint which allows only the digits and N/A value to be inserted in the column.
Asked
Active
Viewed 89 times
-1
-
Please edit the question with, what you tried and the issue that you are facing – Noel Feb 10 '16 at 07:59
2 Answers
1
From this article stack overflow article: Oracle 11g - Check constraint with RegEx you can see that regex works on check constraints.
A regex which supports what you want would be:
^([0-9]+|N/A)$
^
at start of expression means start of line/row/text;
[0-9]+
digits 1 to x;
|
or operator;
N/A
a specific text;
$
end of line/row/text

Community
- 1
- 1

Markus Zaczek
- 154
- 6
-
Thanks ... But this regex is allowing me to insert other charaters as well :( – smrita Feb 10 '16 at 08:53
-
I got a problem testing it right now. But i guess i missed the boundaries from where to where the string has to be found. I'll update my answer. – Markus Zaczek Feb 10 '16 at 12:32
0
You can try like this:
CHECK (column IN (REGEXP_LIKE(column, '^[[:digit:]]{9}$'), 'N/A')

Rahul Tripathi
- 168,305
- 31
- 280
- 331