Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIKE pattern T-SQL

I have wrote a phraser which is phrasing a quite long string into small pieces, after the phrasing of one item is completed, it removes him from @input and continue, until it wont be able to find any items to phrase. I am selecting items based on LIKE pattern.

In some cases, there are however it is selecting some other parts of the message, and then it's end in infinitive loop.

The pattern I am looking for to be selected using LIKE clause is in format of :

(Any number from 1 to 9) + (variable length A-Z only) + '/' + (variable length A-Z only)+space of Cr or Lf or 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

Many thanks for help!

like image 872
Petrik Avatar asked Jul 09 '15 13:07

Petrik


People also ask

What is like pattern in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Does T SQL support RegEx?

We use regular expressions to define specific patterns in T-SQL in a LIKE operator and filter results based on specific conditions. We also call these regular expressions as T-SQL RegEx functions. In this article, we will use the term T-SQL RegEx functions for regular expressions.

What is pattern matching in SQL LIKE operator?

SQL pattern matching enables you to use _ to match any single character and % to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. Do not use = or <> when you use SQL patterns.

Can LIKE operator be used for pattern matching?

You can use the Like operator to find values in a field that match the pattern you specify. For pattern, you can specify the complete value (for example, Like “Smith”), or you can use wildcard characters to find a range of values (for example, Like “Sm*”).


1 Answers

Try changing your match variable/criteria to this:

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

This will get you the desired result. Loosely translated it is saying "Get me the starting position of the first match where the pattern is [anything]-[number from 1-9]-[single letter from a-z]-[anything]-[slash]-[single letter from a-z]-[anything].
Hope this helps!

like image 85
How 'bout a Fresca Avatar answered Oct 13 '22 10:10

How 'bout a Fresca