Hello,
With this regex : not REGEXP_LIKE (categories, '[^(subcat1| subcat2 |subcat3|null; )]', 'i'), i can specified what kind of values i can be used and this are separated with ';'.
but i can do that
insert into regexText Values(';')
insert into regexTest Values('subcat1 subcat2)
insert into regexTest Values ('subcat1;)
insert into regexTest values('subcat1;null)
but insert into regexTest Values ('null') is allowed
and i want to correct my regex so that this insertion are not allowed.
Therefore i do that
Create Table regexTest(
categories VARCHAR2(1000) constraints check_ValidValue check(not regexp_like(categories,'^[(subcat1|subcat2|subcat3|null)];[(subcat1|subcat2|subcat3)]*;[(subcat1|subcat2|subcat3)]*'))
)
but this did not work how i have expected.
----------------------------------------------------------------------------------------------------------------------------------
Hi chris ,
the purpose of this task is to add comma delimited string in a cell.
I have to determine a list of value that can be add in a cell.Although there are many string to be add.
e.g : the allowed value are : subcat1, subcat2,subcat3,subcat4.
i can insert in a cell : subcat1;subcat2;subcat3 Or subcat1 or subcat2;subcat1 or the value null. And when i insert the value null i can not insert anything else.
I can not insert: subcat1;subcat5 or subcat5 or subcat1; or ;
This symbols are to be allowed in the string : &,- and spaces.
And after ';' i can used spaces or not.
allowed values : subcat1
subcat2
subcat3
subcat4
The Data
A & B
A-B
Code that meets requirements :
insert into regexText Values('subcat1')
insert into regexText Values('subcat1; subcat3')
insert into regexText Values('subcat3; subcat4')
insert into regexText Values('null')
insert into regextext Values ('the data; subcat4)
insert into regexText Values('A & B; the data')
insert into regexText Values('A & B; A-B; subcat2')
Code that does not meet requirements:
insert into regexText Values(';')
insert into regexText Values('subcat1;')
insert into regexText Values('null; subcat4')
insert into regexText Values('subcat4; null')
insert into regexText Values('subcat7')
insert into regexText Values('subcat1; subcat6)
I thought that with a regex i can solve this problem but it appears difficult.
I don't know if there are an another way to solve this problem.
Thanks for the answer
Seriously. Rethink your data model. Storing comma, colon or any other character separated values is horrible, terrible idea. This will cause you pain later down the line. Using the string "null" instead of the value null is also questionable.
Anyway, if you know what the allowable values are, specify them using an IN list:
set define off
create table regexText (
x varchar2(20)
);
alter table regexText add constraint c check (
x in ('subcat1', 'subcat1; subcat3','subcat3; subcat4', 'null',
'the data; subcat4', 'A & B; the data', 'A & B; A-B; subcat2')
);
Or, for a more flexible, data driven approach, create a lookup table of allowed values. Then place a foreign key from your table to this lookup:
create table allowed_vals (
x varchar2(20) not null primary key
);
insert into allowed_vals Values('subcat1') ;
insert into allowed_vals Values('subcat1; subcat3') ;
insert into allowed_vals Values('subcat3; subcat4') ;
insert into allowed_vals Values('null') ;
insert into allowed_vals Values ('the data; subcat4');
insert into allowed_vals Values('A & B; the data') ;
insert into allowed_vals Values('A & B; A-B; subcat2') ;
commit;
alter table regexText add constraint fk
foreign key (x) references allowed_vals (x);