mssql sqlserver 验证整型函数共享

发布时间:2019-10-19  栏目:MySQL  评论:0 Comments

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

 

留下评论

网站地图xml地图