mssql sqlserver 验证整型函数分享

转自: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

 

posted @ 2018-08-14 17:34  Adeal2008  阅读(420)  评论(0编辑  收藏  举报