user3575612
user3575612

Reputation: 63

Sql query for like operator

I have a search box where user enters anything in textbox and submits.Lets say user enters Peter.If we are comparing in sql table with following query

select * from xml where name like '%peter%'

This query works well,but when i want to search for only peter in the string,then it creates a problem.Like when i want to search for only those rows which are having Peter present in the name column. in name column,value 1-My name is Peter. valu2-My name is Peterwatter. valu3-My name is Peter living in US. when i want to search it should give only first and third row.not the second one.BUt with above query i m getting all the three rows.How can i correct that.Please guide on this

Upvotes: 1

Views: 76

Answers (4)

Syed Mauze Rehan
Syed Mauze Rehan

Reputation: 1145

select * from xml 
 or name like '%Peter.'
 or name like 'Peter%'
 or name like 'Peter %'
 or name like '% Peter.'
 or name like '% Peter %'

Edit:: Now it will.

Upvotes: 0

uncoder
uncoder

Reputation: 1876

There is a way to do so with regular expressions, but a simple solutions can be like this:

[name] like '% peter %' 
or [name] like '% peter' 
or [name] like 'peter %' 
or [name] = 'peter'

Upvotes: 1

b3ko
b3ko

Reputation: 390

try like '% peter %' (with spaces after/before the wild cards) However please watch out for sql injections. http://en.wikipedia.org/wiki/SQL_injection

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

select * from xml 
 where name like '%peter'
    or name like '%peter %'

Upvotes: 3

Related Questions