Ho scritto un frasario che sta formando una stringa abbastanza lunga in piccoli pezzi, dopo che il fraseggio di un elemento è completato, lo rimuove da @input e continua, finché non sarà in grado di trovare alcun elementi per frase. Sto selezionando gli articoli in base al modello LIKE.LIKE pattern T-SQL
In alcuni casi, tuttavia, è in corso la selezione di altre parti del messaggio, quindi termina nel ciclo infinito.
Il modello Sto cercando di essere selezionati mediante clausola LIKE è in formato di:
(qualsiasi numero da 1 a 9) + (lunghezza variabile AZ solo) + '/' + (variabile lunghezza solo AZ) + spazio di Cr o Lf o 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
Molte grazie per l'aiuto!
T-SQL non ha espressioni regolari o qualsiasi altro built-in pattern matcher che farà quello che vuoi. In particolare, le parti "qualsiasi numero di" e "lunghezza variabile" sono ciò che causerà problemi. –
Quindi nella tua nuova corrispondenza, per '@ input1',' @ position' dovrebbe essere 1 e per '@ input2',' @ position' dovrebbe essere 0? Corretta? –
Non è possibile analizzare tale input utilizzando le funzionalità limitate di PATINDEX. È necessario utilizzare espressioni regolari nel codice o scrivere un parser effettivo. –