escape square brackets in PATINDEX with SQL Server

為{幸葍}努か 提交于 2019-12-10 13:35:11

问题


This doesn't return the expected results. Not sure how I can escape the left and right square brackets, so that PATINDEX takes them into account.

Any clues? Many thanks.

SELECT 
    PATINDEX('%[[SQLSERV]].DBNAME.DBO.[[[0-9a-zA-Z]]]%','ert[SQLSERV].DBNAME.DBO.[Table name]asdadsf')

This should return 3 but it returns 0.


回答1:


Apparently closing brackets don't need to be escaped:

SELECT 
    PATINDEX('%[[]SQLSERV].DBNAME.DBO.[[][0-9a-zA-Z _-]%','ert[SQLSERV].DBNAME.DBO.[Table name]asdadsf')

the above returns 4.

Also, we can't customise an escape character coupled with PATINDEX as we can do with LIKE.



来源:https://stackoverflow.com/questions/32403133/escape-square-brackets-in-patindex-with-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!