使用 SQL Server 在 PATINDEX 中转义方括号 [英] escape square brackets in PATINDEX with SQL Server

查看:33
本文介绍了使用 SQL Server 在 PATINDEX 中转义方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不会返回预期的结果.不知道如何转义左右方括号,以便 PATINDEX 将它们考虑在内.

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')

这应该返回 3 但它返回 0.

This should return 3 but it returns 0.

推荐答案

显然右方括号不需要转义:

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')

以上返回4.

此外,我们不能像使用 LIKE 那样自定义带有 PATINDEX 的转义字符.

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

这篇关于使用 SQL Server 在 PATINDEX 中转义方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆