首页 > 解决方案 > 如何使用 contains 来查找包含空格的关键字?

问题描述

我想在contains搜索时使用一个左右两边都有一个空格的单词。但它不工作..

SELECT *
FROM mytable
WHERE Contains(name, ' Hertz ');

任何帮助表示赞赏..

标签: sqlsql-servercontains

解决方案


You Can LTRIM RTRIM the column and can get the values.

create table #temp
(
name varchar(50)
)

insert into #temp values(' hertz')

insert into #temp values('hertz ')

insert into #temp values('hertzx')

insert into #temp values('hertz')

select * from #temp where LTRIM(RTRIM(name)) like '%hertz%'

If you want to search if it has space in start or end use below query.

select * from #temp where name like ' %'

select * from #temp where name like '% '

推荐阅读