How to retrieve only the 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')
Output I need:
Name
-----
113
675
Taking both most upvoted answer on the duplicate target and removing a .
from the code, plus a comment from that answer to Why are non-digits LIKE [0-9]?, the accurate answer to this question about integers is this:
SELECT Name
FROM tbl_data
WHERE Name NOT LIKE '%[^0123456789]%' COLLATE Latin1_General_CS_AS
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
Output:
Name
113
675
Select name from tbl_data where Isnumeric(name) = 1 and name not like '%.%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With