Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Suppose  I have a table 'tbl' which have column 'ProductName' with multiple records. Now I want to search text in this way that user type 'j' then in every case it show ProductName which start from j letter (we can do like condition 'j%')  but when user type 'Jeans' then it show ProductName which have jeans word 'Jeans', 'Levis Jeans', 'Denim Kids Jeans', 'Flat Jeans New' (Please see below table structure).I am showing these searching text in 'textbox' using 'Ajax AutoCompleteExtender' control. So all searching text show in frontend.

What my Requirement : If user write single letter like 'j' as below mentioned example, then all the search comes which has 'j' letter first. But if user write some proper word like ('jeans','Phone') then it show result on the basis of these word in all the column where it gets these word.

ID   ProductName

1    Cell Phone
2    Shoes
3    Jeans
4    Jug
5    Gift
6    Art
7    Levis Jeans
8    Denim Kids Jeans
9    IPhone new handset
10   Flat Jeans New  

 Case 1: Now If user write only 'j' then it show result like 

ID   ProductName

3    Jeans
4    Jug

 Case 2: but If user write 'jeans' then it show result like

ID   ProductName

3    Jeans
7    Levis Jeans
8    Denim Kids Jeans
10   Flat Jeans New  

or If user write 'phone' then it show result like

ID   ProductName

1    Cell Phone
9    IPhone new handset

Any Suggestion really appreciate.
Posted
Updated 15-May-13 18:38pm
v3
Comments
Jignesh Khant 15-May-13 4:25am    
use 'like' operator of sql like this
SELECT * FROM tbl WHERE ProductName LIKE 'j%';
Mas11 15-May-13 4:48am    
Please read my question properly !
[no name] 15-May-13 8:51am    
Is it "FULL TEXT SEARCH" which u r looking for..??
Mas11 16-May-13 0:36am    
No, Is it not full text search. It is, If user write single letter like 'j' as above mentioned example, then all the search comes which has 'j' letter first. But if user write some proper word like ('jeans','Phone') then it show result on the basis of these word in all the column where it get these word.

May This Help You

SQL
CREATE  PROC[dbo].[Product_Search] 
(@KeyWord AS VARCHAR(MAX))
AS

DECLARE @qSel    AS VARCHAR(MAX)
DECLARE @qWhere  AS VARCHAR(MAX)
DECLARE @query   AS VARCHAR(MAX)


SET @qSel = 'SELECT * FROM [Products] cc '

SET @qWhere = ' '

	
IF LEN(@KeyWord) > 1
BEGIN
    SET @qWhere = @qWhere + 'cc.ProductName  Like ''%' + @KeyWord + '%'' '
END
ELSE
BEGIN
    SET @qWhere = @qWhere + 'cc.ProductName  Like ''' + @KeyWord + '%'' '
END		



IF LEN(@qWhere) != 0
    SET @qWhere = 'WHERE ' + @qWhere	
	
	
SET @query = @qSel + @qWhere 

PRINT (@query)
EXEC (@query)


if you enter 'j' then the result will base on this query

SQL
SELECT * FROM [Products] cc WHERE  cc.ProductName  Like 'j%' 


if you enter 'jeans' then the result will base on this query

SQL
SELECT * FROM [Products] cc WHERE  cc.ProductName  Like '%jeans%' 


I Check Length of Key Word in
SQL
IF LEN(@KeyWord) > 1
you can change it if you want it depends on your requirement . can also add order by clause if you want by changing the following line.

SQL
SET @query = @qSel + @qWhere + ' order by cc.ProductName ASC '

or any other Column
 
Share this answer
 
v2
Comments
Mas11 16-May-13 4:53am    
Wonderful Abdul ! You creates smile on my face. Good job bro & its mine answer.
abdussalam143 16-May-13 5:03am    
If I am Help full for any one its a big pleasure.
Mas11 16-May-13 9:52am    
ya ! You are.
once see the link it may be use full for you.....
Implementation Example of Ranked Search in ASP.NET[^]
 
Share this answer
 
Comments
Mas11 16-May-13 0:39am    
Thanks for your answer but its not that one which I m looking for.

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