SQL Server 具有 LIKE 运算符来处理通配符搜索。我的客户希望在应用程序的用户界面中使用“*”(星号)字符作为通配符。我只是想知道在执行 LIKE 通配符搜索之前,除了“%”(百分比)字符本身之外,我是否需要担心任何标准字符(在 SQL Server 中用作特殊字符),以防它们的关键字包含“%”,需要在实际字符串中找到一个“%”。如果是这样,它们是什么?
所以请假设 [table1].[column1] 在文本字符串中永远不会有“*”(星号)!
这是我到目前为止所拥有的。"%"
除了标准角色和习惯之外,我是否需要处理更多的情况?"*"
-- custom replacement
select REPLACE('xxx*xxx', '*', '%')
-- standard replacements
select REPLACE('xxx%xxx', '%', '[%]')
select REPLACE('xxx_xxx', '_', '[_]') -- ???
select REPLACE('xxx[xxx', '[', '[[]') -- ???
select REPLACE('xxx]xxx', ']', '[]]') -- ???
例子:
SET @p = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@p, ']', '[]]'), '[', '[[]'), '_', '[_]'), '%', '[%]'), '*', '%')
SELECT 'xxxxxxxxx%xxxxxx' LIKE @p
SELECT [table1].[column1] LIKE @p