Ask The Oracle Expert: Questions & Answers

How can SQL test for pure numerics?

How can SQL test for pure numerics?

By  Rudy Limeback, SQL Consultant, r937.com

SearchOracle.com

One of the mysteries I have never been able to solve is how to test a field for pure numerics. We have instances where an attribute's contents determine the processing. Using COBOL I would write "if not NUMERIC". What is the SQL equivalent?

If it's a numeric datatype column, you don't have to test it at all. <grin>

So the question of how to test for pure numerics is relevant only to string datatype columns, like CHAR and VARCHAR. And the specific method is pretty much dictated by whatever functions may be available in your particular DBMS.

In DBMSs which have one, like SQL Server, use the ISNUMERIC() function. In MySQL and PostgreSQL, use a regular expression such as '^[0-9.]$'. Note that the decimal point should be considered one of the valid "numeric" characters.