How to get only integer values ​​from SQL Server

How to get only integer value from this column?

My table:

CREATE TABLE tbl_data (
    [Name] nvarchar(max)
)

INSERT INTO tbl_data VALUES
('Nikesh'),
('113'),
('sunam'),
('sudhir'),
('2.30'),
('ankit'),
('675')

The result I need is:

Name
-----
113
675
+4
source share
4 answers

Taking both the highest priority response to the duplicated target and deleting .from the code, as well as a comment from this answer to Why there are no LIKE digits [0-9]? , the exact answer to this question about integers is this:

SELECT Name
FROM tbl_data
WHERE Name NOT LIKE '%[^0123456789]%' COLLATE Latin1_General_CS_AS
+6
source

You can use the IsNumeric function.

: https://msdn.microsoft.com/en-us/library/ms186272.aspx

Msdn :

USE master;  
GO  
SELECT name, isnumeric(name) AS IsNameANumber, database_id, isnumeric(database_id) AS IsIdANumber   
FROM sys.databases;  
GO  

, . .

+3
Select name from tbl_data where Isnumeric(name) = 1 and name not like '%.%'
+3
source

You can use XML.value :

;WITH tbl_data AS (
SELECT *
FROM (VALUES
('Nikesh'),
('113'),
('sunam'),
('sudhir'),
('2.30'),
('ankit'),
('675')
) as t([Name])
)

SELECT *
FROM tbl_data
WHERE CAST([Name] as xml).value('. cast as xs:integer?','int') IS NOT NULL

Conclusion:

Name
113
675
+3
source

Source: https://habr.com/ru/post/1658604/


All Articles