regext-sql

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


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.


Solution

  • 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.