This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to check the number is an Integer or not an Integer.
Input data: ‘1’
Expected Output:
1 2 3 |
IsInteger -------------- An Integer |
Input data: ‘8a’
Expected Output:
1 2 3 |
IsInteger -------------- Not an Integer |
Solution 1:
1 2 3 4 5 6 7 8 9 10 11 |
DECLARE @str AS VARCHAR(10) = '1' SELECT CASE WHEN SUM(CASE WHEN ASCII(t) BETWEEN 48 AND 57 THEN 1 END) = SUM(1) THEN 'An Integer' ELSE 'Not an Integer' END IsInteger FROM ( SELECT DISTINCT SUBSTRING(@str,Number,1) t FROM Master..Spt_Values WHERE Number > 0 AND Number <= DATALENGTH(@str) )t1 GO |
Solution 2: Using TRY_PARSE
1 2 3 4 5 6 |
DECLARE @str AS VARCHAR(10) = '8a' IF TRY_PARSE(@str AS INT) IS NULL SELECT 'Not an Integer' AS IsInteger ELSE SELECT 'An Integer' AS IsInteger GO |
Please try the different solution for this puzzle and share it via comment...