How do you search for a & (ampersand) in the tsql contains function?

徘徊边缘 提交于 2019-12-31 02:11:10

问题


I have a table T which has a column C which contains text that have the character & within it, but if I do the following query, it returns nothing, although I may have 10 rows.

SELECT * FROM T WHERE Contains(C, 'a&b')

回答1:


Use double quotes to make it a search term:

SELECT *
FROM T
WHERE CONTAINS(C, '"a&b"')

Otherwise you are searching for something that has a AND b.




回答2:


Use like instead:

SELECT * FROM T WHERE C like '%&%'

if you're looking just for &

or

SELECT * FROM T WHERE C like '%a&b%'

if you need to search for a&b




回答3:


Could you use a LIKE instead of contains?

SELECT * FROM T WHERE C LIKE '%a&b%'



回答4:


What I do is create a copy of the column that replaces & and other non-alphanumeric characters with _. Then I can do this.

SELECT * FROM T WHERE Contains(C_searchable, 'a_b')


来源:https://stackoverflow.com/questions/714980/how-do-you-search-for-a-ampersand-in-the-tsql-contains-function

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