如何仅从此列中检索整数值?
我的表:
CREATE TABLE tbl_data (
[Name] nvarchar(max)
)
INSERT INTO tbl_data VALUES
('Nikesh'),
('113'),
('sunam'),
('sudhir'),
('2.30'),
('ankit'),
('675')
我需要的输出:
Name
-----
113
675
答案 0 :(得分:6)
从最受欢迎的answer on the duplicate target和从代码中移除.
,再加上Why are non-digits LIKE [0-9]?的答案中的评论,这个关于整数的问题的准确答案是:
SELECT Name
FROM tbl_data
WHERE Name NOT LIKE '%[^0123456789]%' COLLATE Latin1_General_CS_AS
答案 1 :(得分:3)
您可以使用IsNumeric功能。
有关详情,请访问: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
但请记住,您的设计看起来不太好。每列只应存储一种数据类型。
由于
答案 2 :(得分:3)
Select name from tbl_data where Isnumeric(name) = 1 and name not like '%.%'
答案 3 :(得分:3)
您可以使用XML.value:
$cat.on("change",function(){
var _rel = $(this).val();
$subcat.find("option").attr("style","");
$subcat.val("");
if(!_rel) {
$subcat.prop("disabled", true);
$subcat.selectpicker('refresh');
return;
}
$subcat.find("[rel!="+_rel+"]").hide();
$subcat.prop("disabled", false);
$subcat.selectpicker('refresh');
});
输出:
;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