Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CHECK CONSTRAINT of string to contain only digits. (Oracle SQL)

Tags:

sql

oracle

I have a column, say PROD_NUM that contains a 'number' that is left padded with zeros. For example 001004569. They are all nine characters long.

I do not use a numeric type because the normal operation on numbers do not make sense on these "numbers" (For example PROD_NUM * 2 does not make any sense.) And since they are all the same length, the column is defined as a CHAR(9)

CREATE TABLE PRODUCT (
    PROD_NUM CHAR(9) NOT NULL
    -- ETC.
)

I would like to constrain PROD_NUM so it can only contain nine digits. No spaces, no other characters besides '0' through '9'

like image 343
Shannon Severance Avatar asked Jul 22 '09 19:07

Shannon Severance


People also ask

How do I check if a string contains only numbers in SQL?

SQL Server ISNUMERIC() Function The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.

How do you check if a string is numeric in Oracle?

Answer: To test a string for numeric characters, you could use a combination of the LENGTH function, TRIM function, and TRANSLATE function built into Oracle. The string value that you are testing.

How do I count characters in a string in Oracle?

The Oracle REGEXP_COUNT function is used to count the number of times that a pattern occurs in a string. It returns an integer indicating the number of occurrences of a pattern. If no match is found, then the function returns 0.

Can we use regex in Oracle SQL query?

In Oracle Database 10g, you can use both SQL and PL/SQL to implement regular expression support. Regular expressions are a method of describing both simple and complex patterns for searching and manipulating. String manipulation and searching contribute to a large percentage of the logic in a Web-based application.


1 Answers

REGEXP_LIKE(PROD_NUM, '^[[:digit:]]{9}$')
like image 151
Tim Sylvester Avatar answered Oct 16 '22 18:10

Tim Sylvester