创建函数

在SQL中使用正规表达式CREATE FUNCTION dbo.find_regular_expression
在SQL中使用正规表达式 (
在SQL中使用正规表达式  
@source varchar(5000),   --需要匹配的源字符串
在SQL中使用正规表达式
  @regexp varchar(1000),  --正则表达式
在SQL中使用正规表达式
  @ignorecase bit = 0  --是否区分大小写,默认为false
在SQL中使用正规表达式
 )
在SQL中使用正规表达式
RETURNS bit  --返回结果0-false,1-true
在SQL中使用正规表达式
AS
在SQL中使用正规表达式 
BEGIN
在SQL中使用正规表达式
在SQL中使用正规表达式 
--0(成功)或非零数字(失败),是由 OLE 自动化对象返回的 HRESULT 的整数值。
在SQL中使用正规表达式
  DECLARE @hr integer 
在SQL中使用正规表达式
在SQL中使用正规表达式
--用于保存返回的对象令牌,以便之后对该对象进行操作
在SQL中使用正规表达式
  DECLARE @objRegExp integer   DECLARE @objMatches integer
在SQL中使用正规表达式
在SQL中使用正规表达式
--保存结果
在SQL中使用正规表达式
  DECLARE @results bit
在SQL中使用正规表达式  
END


测试用例

在SQL中使用正规表达式DECLARE @intLength AS INTEGER
在SQL中使用正规表达式
DECLARE @vchRegularExpression AS VARCHAR(50)
在SQL中使用正规表达式
DECLARE @vchSourceString as VARCHAR(50)
在SQL中使用正规表达式
DECLARE @vchSourceString2 as VARCHAR(50)
在SQL中使用正规表达式
DECLARE @bitHasNoSpecialCharacters as BIT
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 初始化变量
在SQL中使用正规表达式
SET @vchSourceString = 'Test one This is a test!!'
在SQL中使用正规表达式
SET @vchSourceString2 = 'Test two This is a test'
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 我们的正则表达式应该类似于
在SQL中使用正规表达式--
 [a-zA-Z ]{}
在SQL中使用正规表达式--
 如: [a-zA-Z ]{10}  在SQL中使用正规表达式  一个十字符的字符串
在SQL中使用正规表达式

在SQL中使用正规表达式
-- 获得字符串长度
在SQL中使用正规表达式
SET @intLength = LEN(@vchSourceString)
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 设置完整的正则表达式
在SQL中使用正规表达式
SET @vchRegularExpression = '[a-zA-Z ]{' + CAST(@intLength as varchar+ '}'
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 是否有任何特殊字符
在SQL中使用正规表达式
SET @bitHasNoSpecialCharacters = dbo.find_regular_expression(@vchSourceString@vchRegularExpression,0)
在SQL中使用正规表达式
在SQL中使用正规表达式
PRINT @vchSourceString
在SQL中使用正规表达式
IF @bitHasNoSpecialCharacters = 1 BEGIN
在SQL中使用正规表达式 
PRINT 'No special characters.'
在SQL中使用正规表达式
END ELSE BEGIN
在SQL中使用正规表达式 
PRINT 'Special characters found.'
在SQL中使用正规表达式
END
在SQL中使用正规表达式
在SQL中使用正规表达式
PRINT '**************'
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 获得字符串长度
在SQL中使用正规表达式
SET @intLength = LEN(@vchSourceString2)
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 设置完整的正则表达式
在SQL中使用正规表达式
SET @vchRegularExpression = '[a-zA-Z ]{' + CAST(@intLength as varchar+ '}'
在SQL中使用正规表达式
在SQL中使用正规表达式
-- 是否有任何特殊字符
在SQL中使用正规表达式
SET @bitHasNoSpecialCharacters = dbo.find_regular_expression(@vchSourceString2@vchRegularExpression,0)
在SQL中使用正规表达式
在SQL中使用正规表达式
PRINT @vchSourceString2
在SQL中使用正规表达式
IF @bitHasNoSpecialCharacters = 1 BEGIN
在SQL中使用正规表达式 
PRINT 'No special characters.'
在SQL中使用正规表达式
END ELSE BEGIN
在SQL中使用正规表达式 
PRINT 'Special characters found.'
在SQL中使用正规表达式
END
在SQL中使用正规表达式
在SQL中使用正规表达式
GO

相关文章:

  • 2021-10-30
  • 2021-09-22
  • 2021-07-04
  • 2022-02-09
  • 2021-11-14
  • 2021-12-11
  • 2021-12-08
猜你喜欢
  • 2021-11-14
  • 2021-10-19
  • 2021-07-04
  • 2022-01-01
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案