SQL Server, combining % and [] wildcards in LIKE statement

后端 未结 2 700
旧时难觅i
旧时难觅i 2021-01-02 11:06

In t-sql, is there a way to do pattern matching in a like statement such that you can search for 1 or more characters in a given set?

To be specific, I\'m trying to

相关标签:
2条回答
  • 2021-01-02 11:36

    Like pattern matching is very limited, it does not allow for normal regular expressions.

    See here for details.

    For your needs use:

    WHERE ColumnName LIKE '[a-z]%[0-9]'
    

    This will match any letter followed by anything, followed by a number. SQL will enforce that the letter and number are at the two ends of the string because there is no pattern or literal character before or after our [] match set.

    0 讨论(0)
  • 2021-01-02 11:38

    If you want to use Regex and have permissions, you can write a user defined function to give yourself access to the Regular Expressions parser on the SQL server's .NET CLR:

    http://msdn.microsoft.com/en-us/magazine/cc163473.aspx (SQL 2005 and higher)

    0 讨论(0)
提交回复
热议问题