How to select only numeric values

大城市里の小女人 提交于 2019-12-08 15:11:23

问题


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


回答1:


Try ISNUMERIC

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

SQLFiddle Demo




回答2:


SELECT * FROM @Table 
WHERE Col NOT LIKE '%[^0-9]%' 



回答3:


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



回答4:


 SELECT column1 FROM table where ISNUMERIC(column1) = 1



回答5:


You can use translate and replace function together. first translate the numbers to 0 then replace them with null and return only null values can solve the problem.

select column from table where  replace(translate(column,'0123456789','0000000000'),'0','') is null 


来源:https://stackoverflow.com/questions/12659142/how-to-select-only-numeric-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!