轉自:http://www.maomao365.com/?p=6227
摘要:
下文將製作一個isnumber驗證整型的函式,供在sql指令碼中做數值判斷,如下所示:
例:
實現原理:判斷 是否包含特殊字元,是否全部為0-9的自然陣列成
CREATE FUNCTION dbo.check_number ( /*驗證一字串型別是否為整型數值型別*/ @checkTmp VARCHAR(100) ) RETURNS BIT BEGIN DECLARE @tmp BIT IF LEFT(@checkTmp, 1) = `-` SET @checkTmp = SUBSTRING(@checkTmp, 2, LEN(@checkTmp)) IF (PATINDEX(`%[^0-9-]%`, @checkTmp) = 0 AND CHARINDEX(`-`, @checkTmp) <= 1 AND @checkTmp NOT IN (`.`, `-`, `+`, `^`) AND LEN(@checkTmp)>0 AND @checkTmp NOT LIKE `%-%`) SET @tmp = 1 ELSE SET @tmp = 0 RETURN @tmp END GO /*資料驗證*/ SELECT `878` [測試數值],dbo.check_number(`999`) [測試返回值]; SELECT `testData` [測試數值],dbo.check_number(`abc`) [測試返回值]; SELECT `8-8` [測試數值],dbo.check_number(`9+9`) [測試返回值]; SELECT `8.9` [測試數值],dbo.check_number(`$9.9`) [測試返回值]; SELECT `maomao365.com` [測試數值],dbo.check_number(`maomao365.com`) [測試返回值]; go drop function check_number