Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 12-Nov-17 23:22pm

Short answer - you cannot - cast &/or convert will return an error if the value fails to convert to an integer.
Long answer - the below will work;
SQL
-- 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
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900