Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select only numeric values

Table1

id

01
wire
02
steve
ram123
03
....

from the table1 i want to select only numeric values, It should not display alphanumeric values like (ram123)

Expected Output

01
02
03
....

How to make a query for this condition

like image 914
JetJack Avatar asked Sep 30 '12 06:09

JetJack


3 Answers

Try ISNUMERIC

SELECT *
FROM Table1
WHERE ISNUMERIC([ID]) = 1

SQLFiddle Demo

like image 128
John Woo Avatar answered Oct 12 '22 11:10

John Woo


SELECT * FROM @Table 
WHERE Col NOT LIKE '%[^0-9]%' 
like image 35
highwingers Avatar answered Oct 12 '22 11:10

highwingers


Just want to note that IsNumeric() has some limitations. For example all of the below will return 1.

SELECT ISNUMERIC(' - ')
SELECT ISNUMERIC(' , ')
SELECT ISNUMERIC('$')
SELECT ISNUMERIC('10.5e-1')
SELECT ISNUMERIC('$12.09')

So if you only looking to select numbers ONLY, then something like this could work:

create function [dbo].[IsNumbersOnly](@strSrc as varchar(255))
returns tinyint
as
begin

    return isnumeric(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
        @strSrc, '\', 'x'), '-', 'x'), ',', 'x'), '+', 'x'), '$', 'x'), '.', 'x'), 'e', 'x'), 'E', 'x'),
        char(9), 'x'), char(0), 'x'))
end
like image 1
Void Ray Avatar answered Oct 12 '22 13:10

Void Ray