0

I have an application where a code like YYY/YY/YY or YYY.YY.YY is inserted before it was only with "."

So the previous developer add a constraint in the corresponding table that I drop in TEST environment but I want to add a new one

ALTER TABLE DEPLOYMENT_ADM.DEPLOYMENT 
  ADD CONSTRAINT deployment_chk_billing 
  CHECK (   BILLING_CODE LIKE '[0-9]{3}/[0-9]{2}/[0-9]{2}' 
         OR BILLING_CODE LIKE '[0-9]{3}\\.[0-9]{2}\\.[0-9]{2}');

But when I try it I have

ORA-02293: cannot validate (DEPLOYMENT_ADM.DEPLOYMENT_CHK_BILLING) - check constraint violated

So I don't see where is the error

1
  • LIKE does not support regular expressions in SQL Commented Nov 5, 2019 at 15:33

1 Answer 1

1

You want regexp_like():

ALTER TABLE DEPLOYMENT_ADM.DEPLOYMENT ADD CONSTRAINT deployment_chk_billing
 CHECK (regexp_like(BILLING_CODE, '^[0-9]{3}/[0-9]{2}/[0-9]{2}|[0-9]{3}\\.[0-9]{2}\\.[0-9]{2}$');

EDIT:

I wanted to point out the LIKE was not correct with wildcards. I think the regular expression you want is:

ALTER TABLE DEPLOYMENT_ADM.DEPLOYMENT 
  ADD CONSTRAINT deployment_chk_billing 
  CHECK ( REGEXP_LIKE(BILLING_CODE, '([0-9]{3}/[0-9]{2}/[0-9]{2})|([0-9]{3}[.][0-9]{2}[.][0-9]{2})' ) );

Or if you don't care if . and / are in the same value:

ALTER TABLE DEPLOYMENT_ADM.DEPLOYMENT 
  ADD CONSTRAINT deployment_chk_billing 
  CHECK ( REGEXP_LIKE(BILLING_CODE, '[0-9]{3}[./][0-9]{2}[./][0-9]{2})) );
Sign up to request clarification or add additional context in comments.

5 Comments

(regexp_like(BILLING_CODE, '^[0-9]{3}/[0-9]{2}/[0-9]{2}|[0-9]{3}\\.[0-9]{2}\\.[0-9]{2}$'); 2 open brackets, one closing bracket
I test it and have the same error, but I see that regexp_like statement are used for select, insert, update and delete...
How likely is it that existing data violates the constraint you are trying to add?
Highly likely, @Stilgar, according to error the OP mentioned in their initial message.
I drop the constraint to add a new one so I don't see how a constraint can be violated. Also all data in the column are like the regex expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.