Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have written this query in storeprocedure.But using this I can only search data with complete text.For Example:I can search data whose name='ronak'.i can not search data whose name start with 'r'.

please help me how to do this.


SELECT profile.name1 AS name, profile.address, profile.mob_no, profile.emailid, login.username
FROM profile INNER JOIN
login ON profile.log_id = login.log_id
WHERE (profile.name1 LIKE @name1) OR
(profile.mob_no LIKE @mob_no) OR
(profile.emailid LIKE @emailid) OR
(login.username LIKE @username)

I have try below 2 solution ,but when I write query


SET @name1 = '%' + @name1 + '%'

SELECT profile.name1 AS name, profile.address, profile.mob_no, profile.emailid, login.username
FROM profile INNER JOIN
login ON profile.log_id = login.log_id
WHERE (profile.name1 LIKE @name1) OR
(profile.mob_no LIKE @mob_no) OR
(profile.emailid LIKE @emailid) OR
(login.username LIKE @username)
then it is working for name textbox.And when I write query

SET @name1 = '%' + @name1 + '%'
SET @mob_no = '%' + @mob_no + '%'
SET @emailid = '%' + @emailid + '%'
SET @username = '%' + @username + '%'

SELECT profile.name1 AS name, profile.address, profile.mob_no, profile.emailid, login.username
FROM profile INNER JOIN
login ON profile.log_id = login.log_id
WHERE (profile.name1 LIKE @name1) OR
(profile.mob_no LIKE @mob_no) OR
(profile.emailid LIKE @emailid) OR
(login.username LIKE @username)
then it is not working for any textbox.
Posted
Updated 28-Dec-12 5:43am
v3

Try:

SQL
SELECT profile.name1 AS name, profile.address, profile.mob_no, profile.emailid, login.username
FROM profile INNER JOIN
login ON profile.log_id = login.log_id
WHERE     
(profile.name1 LIKE @name1 +'%') OR
(profile.mob_no LIKE @mob_no +'%') OR
(profile.emailid LIKE @emailid +'%') OR
(login.username LIKE @username +'%')
 
Share this answer
 
If you want to search for any inclusion in a value then you just need to wrap your argument with '%' characters.
SQL
SET @name1 = '%' + @name1 + '%'
SET @mob_no = '%' + @mob_no + '%'
SET @emailid = '%' + @emailid + '%'
SET @username = '%' + @username + '%'

SELECT profile.name1 AS name, profile.address, profile.mob_no, profile.emailid, login.username
  FROM profile INNER JOIN
          login ON profile.log_id = login.log_id
 WHERE profile.name1 LIKE @name1 
    OR profile.mob_no LIKE @mob_no
    OR profile.emailid LIKE @emailid
    OR login.username LIKE @username
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900