Transferred from: http://www.maomao365.com/?p=6227
Summary:
The following is a function that Isnumber validates an integer to make a numerical judgment in a SQL script, as follows:
Cases:
The principle of implementation: to determine whether to include special characters, whether all 0-9 of the natural number composition
CREATE FUNCTIONDbo.check_number (/*verifies whether a string type is an integer numeric type*/@checkTmp VARCHAR( -))RETURNS BITBEGINDECLARE @tmp BITIF Left(@checkTmp,1)= '-'SET @checkTmp = SUBSTRING(@checkTmp,2,LEN(@checkTmp))IF(PATINDEX('%[^0-9-]%',@checkTmp)= 0 and CHARINDEX('-',@checkTmp)<= 1 and @checkTmp not inch('.','-','+','^') and LEN(@checkTmp)>0 and @checkTmp not like '%-%')SET @tmp = 1ELSESET @tmp = 0RETURN @tmpENDGO/*Data Validation*/SELECT '878' [Test Values], Dbo.check_number ('999')[Test return value];SELECT 'TestData' [Test Values], Dbo.check_number ('ABC')[Test return value];SELECT '8-8' [Test Values], Dbo.check_number ('9+9')[Test return value];SELECT '8.9' [Test Values], Dbo.check_number ('$9.9')[Test return value];SELECT 'maomao365.com' [Test Values], Dbo.check_number ('maomao365.com')[Test return value];GoDrop functionCheck_number
MSSQL SQL Server verifies integer function sharing