Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analogue for regex '?' (preceding item optional) in T-SQL like?

Tags:

I am wondering, is it possible to translate regex containing '?' (preceding item optional) in T-SQL LIKE pattern? Without any actions on the DB side. For example, "^31-?4". I could split it up into several clauses, but if regex contains a lot of '?' this is not that convenient.

like image 394
Qué Padre Avatar asked Aug 05 '13 08:08

Qué Padre


People also ask

Does SQL like 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.

Can we use RegEx in SQL Server?

Regular expressions are a concise and flexible notation for finding and replacing patterns of text. A specific set of regular expressions can be used in the Find what field of the SQL Server Management Studio Find and Replace dialog box.

How do I match a string in SQL Server?

SQL Pattern Matching : It is used for searching a string or a sub-string to find certain character or group of characters from a string. We can use LIKE Operator of SQL to search sub-string. The LIKE operator is used with the WHERE Clause to search a pattern in string of column.


1 Answers

LIKE doesn't use regular expressions and the pattern language it uses doesn't have tokens and qualifiers, just a few placeholders:

Wildcard character    Description
------------------    -----------
%                     Any string of zero or more characters.
_ (underscore)        Any single character.
[ ]                   Any single character within the specified range ([a-f]) or set ([abcdef]).
[^ ]                  Any single character not within the specified range ([^a-f]) or set ([^abcdef]).

So no, there isn't such a thing you ask for.

like image 165
Joey Avatar answered Oct 03 '22 11:10

Joey