create table #(name varchar(20)) insert into # select 'aa%aaa' insert into # select 'bbbbb' select * from # where name like '%[%]%' /* aa%aaa */ select * from # where charindex('%',name)>0 /* aa%aaa */
select * from # where name like '%/%%' escape '/'
select * from # where name like '%!%%' escape '!'
select * from table where Content like '%[%]%' --或者: select * from table where Content like '%&%%' escape '&'
charindex() pathindex 一般情况下我很少用like
select * from table where Content like '%[%]%' select * from table where charindex('%',Content)>0select * from table where pathindex('%',Content)>0
一条语句的多条不同写法。 为什么不用LIKE,是效率低吗?
都不错啊不过 1.我测试发现chindex不能用在text,ntext这些字段.like就可以.但用在字符串中是很不错的 2.用[],有些查找是不正确的,因为[]是指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 所以select * from t1 where username like '%[!!![-dddd--]]%' 查找出来的结果有 username ------------ 11[][][] ,这样只要有单个字符[就条件满足,显然跟我们原来的条件不同. 3.用 like ...escape 最符合条件 select * from t1 where username like '%![!!![-dddd--]]%' escape '!'但是用这个如果遇到通配符的话也要用指定的escape符号隔开才有用 select username from t1 where username like '%![]%' escape '!'username ---------------------------- 11[][][](所影响的行数为 1 行) 这样却得不到数据,因为里面的通配符没有隔开 select username from t1 where username like '%!11[][][]%' escape '!' username ------------------------------------- (所影响的行数为 0 行) 换成隔开的就行了 select username from t1 where username like '%!11![]![]![]%' escape '!' username ----------------------------------------- 11[][][](所影响的行数为 1 行) 效率的问题大概都不多吧.不知道我的测试有没有问题就是.欢迎高手指正
[code] --这样测试的 declare @i int declare @count int declare @temptime int set @temptime=0 set @count=0 set @i=0 while (@i<100) begin declare @time1 datetime set @time1=getdate() select * from testsearch --where TextContent like '%[%]%' ---不能用where charindex('%',TextContent)>0 where TextContent like '%!%%' escape '!' set @temptime=datediff(millisecond,@time1,getdate()) set @count=@count+@temptime select '平均时间为'=@count/100 set @i=@i+1 end go [/code]
--这样测试的 declare @i int declare @count int declare @temptime int set @temptime=0 set @count=0 set @i=0 while (@i<100) begin declare @time1 datetime set @time1=getdate() select * from testsearch --where TextContent like '%[%]%' ---不能用where charindex('%',TextContent)>0 where TextContent like '%!%%' escape '!' set @temptime=datediff(millisecond,@time1,getdate()) set @count=@count+@temptime select '平均时间为'=@count/100 set @i=@i+1 end go
select * from table where Content like '\%%'escape'\' 搜出以%开头的任何字符如'%abc'select * from table where Content like '%\%%'escape'\' 搜出包含%的任何字符如'abc%def'select * from table where Content like '\%\%\%'escape'\' 搜出只有三个%的元组其他类推
使用 ESCAPE 关键字定义转义符 这样就可以了 如: Select * From UserInfo Where Address like '%/%%' escape '/'
e.g:% 包含零个或多个字符的任意字符串。 WHERE title LIKE '%computer%' 将查找在书名中任意位置包含单词 "computer" 的所有书名。 _(下划线) 任何单个字符。 WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。 [ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 WHERE au_lname LIKE '[C-P]arsen' 将查找以 arsen 结尾并且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如 Carsen、Larsen、Karsen 等。[^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。 WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始并且其后的字母不为 l 的所有作者的姓氏。
insert into # select 'aa%aaa'
insert into # select 'bbbbb'
select * from # where name like '%[%]%'
/*
aa%aaa
*/
select * from # where charindex('%',name)>0
/*
aa%aaa
*/
select * from table where Content like '%[%]%'
--或者:
select * from table where Content like '%&%%' escape '&'
pathindex
一般情况下我很少用like
select * from table where Content like '%[%]%' select * from table where charindex('%',Content)>0select * from table where pathindex('%',Content)>0
为什么不用LIKE,是效率低吗?
1.我测试发现chindex不能用在text,ntext这些字段.like就可以.但用在字符串中是很不错的
2.用[],有些查找是不正确的,因为[]是指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。
所以select * from t1 where username like '%[!!![-dddd--]]%'
查找出来的结果有
username ------------
11[][][]
,这样只要有单个字符[就条件满足,显然跟我们原来的条件不同.
3.用 like ...escape 最符合条件
select * from t1
where username like '%![!!![-dddd--]]%' escape '!'但是用这个如果遇到通配符的话也要用指定的escape符号隔开才有用
select username from t1
where username like '%![]%' escape '!'username
----------------------------
11[][][](所影响的行数为 1 行)
这样却得不到数据,因为里面的通配符没有隔开
select username from t1
where username like '%!11[][][]%' escape '!'
username
------------------------------------- (所影响的行数为 0 行)
换成隔开的就行了
select username from t1
where username like '%!11![]![]![]%' escape '!'
username
-----------------------------------------
11[][][](所影响的行数为 1 行)
效率的问题大概都不多吧.不知道我的测试有没有问题就是.欢迎高手指正
--这样测试的
declare @i int
declare @count int
declare @temptime int
set @temptime=0
set @count=0
set @i=0
while (@i<100)
begin
declare @time1 datetime
set @time1=getdate()
select *
from testsearch
--where TextContent like '%[%]%'
---不能用where charindex('%',TextContent)>0
where TextContent like '%!%%' escape '!'
set @temptime=datediff(millisecond,@time1,getdate())
set @count=@count+@temptime
select '平均时间为'=@count/100
set @i=@i+1
end
go
[/code]
--这样测试的
declare @i int
declare @count int
declare @temptime int
set @temptime=0
set @count=0
set @i=0
while (@i<100)
begin
declare @time1 datetime
set @time1=getdate()
select *
from testsearch
--where TextContent like '%[%]%'
---不能用where charindex('%',TextContent)>0
where TextContent like '%!%%' escape '!'
set @temptime=datediff(millisecond,@time1,getdate())
set @count=@count+@temptime
select '平均时间为'=@count/100
set @i=@i+1
end
go
这样就可以了
如:
Select * From UserInfo Where Address like '%/%%' escape '/'
WHERE title LIKE '%computer%' 将查找在书名中任意位置包含单词 "computer" 的所有书名。
_(下划线) 任何单个字符。
WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。
[ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。
WHERE au_lname LIKE '[C-P]arsen' 将查找以 arsen 结尾并且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如 Carsen、Larsen、Karsen 等。[^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。
WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始并且其后的字母不为 l 的所有作者的姓氏。