Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: SQL query that returns rows with only numeric values

Tags:

sql

oracle

I have a field (column in Oracle) called X that has values like "a1b2c3", "abc", "1ab", "123", "156"

how do I write an sql query that returns me only the X that hold pure numerical values = no letters? from the example above would be „123“ and „156“

select X from myTable where ...??

like image 969
Imageree Avatar asked Oct 14 '10 10:10

Imageree


People also ask

How do I get just the numeric value in SQL?

In SQL Server, we can use the ISNUMERIC() function to return numeric values from a column. We can alternatively run a separate query to return all values that contain numeric data.

How do I limit the number of rows returned?

You use the LIMIT clause to constrain the number of rows returned by the query. For example, a SELECT statement may return one million rows. However, if you just need the first 10 rows in the result set, you can add the LIMIT clause to the SELECT statement to retrieve 10 rows.

How do I check if a column 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.

Can we use Rownum in Oracle?

You can use ROWNUM to limit the number of rows returned by a query, as in this example: SELECT * FROM employees WHERE ROWNUM < 10; If an ORDER BY clause follows ROWNUM in the same query, then the rows will be reordered by the ORDER BY clause. The results can vary depending on the way the rows are accessed.


2 Answers

You can use the REGEXP_LIKE function as:

SELECT X  FROM myTable  WHERE REGEXP_LIKE(X, '^[[:digit:]]+$'); 

Sample run:

SQL> SELECT X FROM SO;  X -------------------- 12c 123 abc a12  SQL> SELECT X  FROM SO WHERE REGEXP_LIKE(X, '^[[:digit:]]+$');  X -------------------- 123  SQL>  
like image 198
codaddict Avatar answered Sep 22 '22 05:09

codaddict


If the only characters to consider are letters then you can do:

select X from myTable where upper(X) = lower(X) 

But of course that won't filter out other characters, just letters.

Since Oracle 12c (at least) there has been a built-in function to check whether a character value is numeric: VALIDATE_CONVERSION

select X from myTable where validate_conversion(X as number) = 1 
like image 40
Tony Andrews Avatar answered Sep 19 '22 05:09

Tony Andrews