0

How to check if the string contains just lowercase characters, numbers and the symbol "@" in SQL

Im trying something like this:

ALTER TABLE TABLE1
    ADD CONSTRAINT c1
        CHECK (
            (R1 like '%[abcdefghijklmnobqrstuvwxyz@1234567890]%')
            );
ahm5
  • 633
  • 5
  • 9

2 Answers2

1

You should phrase the check constraint as the column not having any characters which are not lowercase, numbers, or the @ symbol:

ALTER TABLE TABLE1
ADD CONSTRAINT c1
    CHECK (NOT REGEXP_LIKE(R1, '[^abcdefghijklmnobqrstuvwxyz@1234567890]'));
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanx but that does not works with me, idk why but DB2 cant find the function REGEXP_LIKE can you please rewrite it but with the function LIKE – ahm5 May 26 '22 at 22:05
0

Try this:

SELECT 
  S
, '>' || TRANSLATE (S, '*', ' abcdefghijklmnobqrstuvwxyz@1234567890') || '<' AS TRANSLATED
, CASE TRANSLATE (S, '*', ' abcdefghijklmnobqrstuvwxyz@1234567890') 
    WHEN '' THEN 'YES'
    ELSE 'NO'
  END AS RES
FROM (VALUES ' abc', 'abc@123', 'Abc') T (S)
S TRANSLATED RES
abc >* < NO
abc@123 > < YES
Abc >A < NO
Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16