0

I am having an issue with a check constraint reading [A-Z].

here is the code:

drop table test CASCADE CONSTRAINTS; Create table test ( post Varchar2(6), CHECK (post like '[A-Z]') ); insert into test values ('A'); 

And I receive a "check constraint violated" error after trying to insert A. Any feedback would be appreciated. Even if it does work for you because everything is telling me it should work.

4
  • What is your version of SQL (e.g. MySQL, SQL Server, Oracle, Postgres, etc.) ? Commented Apr 22, 2021 at 4:44
  • Oracle version 18.4.0.0.0 @TimBiegeleisen Commented Apr 22, 2021 at 4:50
  • The way you put it, only insert into test values ('[A-Z]') would work. It is regexp_like you're looking for (see Tim's answer). Commented Apr 22, 2021 at 5:12
  • LIKE doesn't support regular expressions in SQL Commented Apr 22, 2021 at 5:34

1 Answer 1

4

Use REGEXP_LIKE:

CREATE TABLE test ( post Varchar2(6), CHECK (REGEXP_LIKE(post, '^[A-Z]+$')) ); 

The pattern ^[A-Z]+$ would match posts which only contain capital letters. If instead you want to flag posts which begin with a capital letter, then use ^[A-Z].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.