本文介绍了Sql将varchar转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我的sql数据是:
T863
356
F5963
563
3659
我想获得的代码在355到564之间
但是我得到了来自T863的错误
如何解决这个问题?
我尝试过:
一些谷歌搜索,但有这种情况的任何一个例子
Hello,
My sql datas are:
T863
356
F5963
563
3659
I want to get codes are between 355 to 564
but i get error from T863
How can i fix this problem?
What I have tried:
some google search but there is any example for this situation
推荐答案
-- Declare a Table variable
DECLARE @MyTableVariable TABLE
(ErrorNumber INT)
-- Populate your Table variable
INSERT INTO @MyTableVariable
SELECT CAST(ErrorNumber AS INT) AS ErrorNumber
FROM MyTable
WHERE ISNUMERIC(ErrorNumber) = 1
-- Query the Table Variable
SELECT ErrorNumber
FROM @MyTableVariable
WHERE ErrorNumber BETWEEN 355 AND 564
亲切的问候
Kind Regards
CREATE TABLE #temp(mixedvalues VARCHAR(10));
INSERT INTO #temp
VALUES('T863'),
('356'),
('F5963'),
('563'),
('3659'),
('340T');
;WITH CTE AS(
SELECT SUBSTRING(mixedvalues,
PATINDEX('%[0-9]%', mixedvalues),
(LEN(mixedvalues)-PATINDEX('%[0-9]%',REVERSE(mixedvalues))+1)) AS
NumericValues FROM #TEMP)
SELECT * FROM CTE WHERE NumericValues between 355 and 1000;
-----------------
NumericValues
--------------
NumericValues
863
356
563
这篇关于Sql将varchar转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!