1

I want to validate a string. The conditions are :

  1. It should be 9 chr long.
  2. first character should be alphabet(in uppercase).
  3. remainig chr should be between 0 and 9
  4. No special character is allowed

I want to verify this using REGEXP_LIKE(). Please suggest.

2
  • 2
    OK. what have you attempted, and where are you stuck? Commented Sep 27, 2021 at 13:21
  • regexp_like(string, '^[A-Z][0-9]') Commented Sep 27, 2021 at 13:35

1 Answer 1

3

You should be able to use the following REGEXP_LIKE to validate your inputs:

REGEXP_LIKE(td.text_value, '^[A-Z][0-9]{8}$')

Explaining the regular expression:

  • ^ - Beginning of String
  • [A-Z] - A Capital Letter
  • [0-9]{8} - Exactly 8 digit characters
  • $ - End of String

Here is a query validating certain use cases:

WITH test_data (text_value) AS
(
  SELECT 'A12345678' FROM DUAL UNION ALL
  SELECT 'a12345678' FROM DUAL UNION ALL
  SELECT 'A1234567' FROM DUAL UNION ALL
  SELECT 'A123456789' FROM DUAL UNION ALL
  SELECT '$12345678' FROM DUAL
)
SELECT td.text_value, 
       CASE WHEN REGEXP_LIKE(td.text_value, '^[A-Z][0-9]{8}$') THEN 'Y' ELSE 'N' END AS VALID
FROM test_data td

Here is the link to Oracle's Regular Expression documentation (Link)

Additionally, here is a DBFiddle running the above query (Link)

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

1 Comment

Thanks a lot for quick response...Its really helpful

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.