LIKE模式T-SQL

Pet*_*rik 7 t-sql sql-server sql-server-2012 sql-like

我已经写了一个措辞,将一个很长的字符串写成小块,在完成一个项目的措辞之后,它将它从@input中移除并继续,直到它找不到任何项目的短语.我正在根据LIKE模式选择项目.

在某些情况下,它会选择消息的其他部分,然后以不定式循环结束.

我希望使用LIKE子句选择的模式格式为:

(1至9的任何数字)+(仅可变长度AZ)+'/'+(仅可变长度AZ)+ Cr或Lf或CrLf的空间.

--This is what I do have: 
DECLARE @match NVarChar(100)
    SET @match  =  '%[1-9][a-z]%'

DECLARE @input1 varchar(max),@input2 varchar(max)  
    SET @input1 ='1ABCD/EFGH *W/17001588 *RHELLO SMVML1C'

DECLARE @position Int 
    SET @position = PATINDEX(@match, @input1);

SELECT @position;

--after the loop- it is also 'catching' the 1C at the end of the string: 

SET @input2     = '*W/17001588 *RHELLO SMVML1C'
SET @position   = PATINDEX(@match, @input2);

SELECT @position

---In order to eliminate this, I have tried to change @match:

SET @match  =  '%[1-9][a-z][/][a-z]%'

SET @position = PATINDEX(@match, @input1);
SELECT @position  --postion is  0, so the first item, that should have been selected, wasn't selected
SET @position   = PATINDEX(@match, @input2);
SELECT @position --postion is  0
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

How*_*sca 1

尝试将您的匹配变量/标准更改为:

SET @match  =  '%[1-9][a-z]%[/][a-z]%'
Run Code Online (Sandbox Code Playgroud)

这将为您带来想要的结果。粗略地翻译它是说“给我第一个匹配的起始位置,其中模式是[任何东西]-[1-9中的数字]-[az中的单个字母]-[任何东西]-[斜杠]-[来自1-9中的单个字母] az]-[任何东西]。
希望这有帮助!