Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql like query

Posted on 2013-01-20
23
Medium Priority
?
312 Views
Last Modified: 2013-01-21
hey guys i am having a problem with my search query

if i type this text in my text box for search : "welding inverter"

no results found

but if i put :"welding" or inverter

it bring up results

here is my part of my query

m2.title LIKE '%" + keyword.Replace(" ", "%") + "%'
0
Comment
Question by:JCWEBHOST
  • 11
  • 9
  • 2
  • +1
23 Comments
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38800087
m2.title LIKE '%" + keyword.Trim().Replace(" ", "%' OR m2.title LIKE '%") + "%'
0
 
LVL 39

Expert Comment

by:appari
ID: 38800094
debug the source code and post the sql after replacing "%" in place of  " ".

check if the keyword is ending with " ", try trimming before replace as follows:

m2.title LIKE '%" + keyword.trim().Replace(" ", "%") + "%'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800134
LIKE will only compare to the full string.
if you want to compare to each part, based on 1 input (variable), check out this article
http://www.experts-exchange.com/Database/Miscellaneous/A_1536-delimited-list-as-parameter-what-are-the-options.html
to get the parmsToList function, could would then be like this:
declare @input varchar(1000)
set @input = 'wedding inverter'
select ...
 from yourtable m2
  join dbo.ParmsToList( @input, ',' ) f
where  m2.title LIKE '%' + f.value + '%'  

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:JCWEBHOST
ID: 38800228
guys try it online it not working

http://www.gas-works.co.za/
0
 

Author Comment

by:JCWEBHOST
ID: 38800229
this i my current sql query

SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name FROM  products p inner JOIN menu as m3 ON m3.id = p.menu_id inner JOIN menu as m2 on m2.id = m3.parent_id inner JOIN menu as m1 on m1.id = m2.parent_id WHERE m1.title LIKE '%" + keyword + "%' OR m2.title LIKE '%" + keyword + "%' OR m3.title LIKE '%" + keyword + "%' OR p.p_name LIKE '%" + keyword + "%' OR p.p_code LIKE '%" + keyword + "%'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800233
can you try my method?
0
 

Author Comment

by:JCWEBHOST
ID: 38800272
how to i implement that code
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800286
I posted the sql script extract, and the function's code used is in the article I linked.
what is the problem, please?
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38800290
SELECT  p.id
       ,p_name
       ,SUBSTRING(p.p_name, 0, 10) AS s_name
FROM    products p
INNER JOIN menu AS m3 ON m3.id = p.menu_id
INNER JOIN menu AS m2 ON m2.id = m3.parent_id
INNER JOIN menu AS m1 ON m1.id = m2.parent_id
WHERE   m1.title LIKE '%' + keyword + '%'
        OR m2.title LIKE '%' + keyword + '%'
        OR m3.title LIKE '%' + keyword + '%'
        OR p.p_name LIKE '%' + keyword + '%'
        OR p.p_code LIKE '%' + keyword + '%'

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38800304
i tried this but sql query where is incorrect

SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name FROM  products p inner JOIN menu as m3 ON m3.id = p.menu_id inner JOIN menu as m2 on m2.id = m3.parent_id inner JOIN menu as m1 on m1.id = m2.parent_id join dbo.ParmsToList( '%" + keyword + "%', ',' ) f where  m2.title LIKE '%' + f.value + '%' OR m2.title join dbo.ParmsToList( '%" + keyword + "%', ',' ) f where  m2.title LIKE '%' + f.value + '%' OR m3.title join dbo.ParmsToList( '%" + keyword + "%', ',' ) f where  m2.title LIKE '%' + f.value + '%' OR p.p_name join dbo.ParmsToList( '%" + keyword + "%', ',' ) f where  m2.title LIKE '%' + f.value + '%' OR p.p_code join dbo.ParmsToList( '%" + keyword + "%', ',' ) f where  m2.title LIKE '%' + f.value + '%'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800323
what about this:
SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name 
FROM  products p 
JOIN menu as m3 ON m3.id = p.menu_id 
JOIN menu as m2 on m2.id = m3.parent_id 
JOIN menu as m1 on m1.id = m2.parent_id 
JOIN dbo.ParmsToList( '%" + keyword + "%', ',' ) f 
  ON m2.title LIKE '%' + f.value + '%' 
  OR m3.title LIKE '%' + f.value + '%'  
  OR m1.title LIKE '%' + f.value + '%'  
  OR p.p_name LIKE '%' + f.value + '%'  
  OR p.p_code LIKE '%' + f.value + '%'  

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38800331
pourfard your code do not work it does not find any records
0
 

Author Comment

by:JCWEBHOST
ID: 38800350
angelIII your last code do not work to. No recordes found
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800383
is there any of the fields that may be null?

can you show the output of this query:
SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name , f.value
FROM  products p 
JOIN menu as m3 ON m3.id = p.menu_id 
JOIN menu as m2 on m2.id = m3.parent_id 
JOIN menu as m1 on m1.id = m2.parent_id 
LEFT JOIN dbo.ParmsToList( '%" + keyword + "%', ',' ) f  ON 1 = 1 

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38800399
The following error occured while executing the query:
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.ParmsToList'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800403
did you create the function? it's not a build-in function
0
 

Author Comment

by:JCWEBHOST
ID: 38800408
i never create. how do i create it?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800432
you have the create function code in the article
0
 

Author Comment

by:JCWEBHOST
ID: 38800608
ok here is the sql query

SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name , f.value
FROM  products p
JOIN menu as m3 ON m3.id = p.menu_id
JOIN menu as m2 on m2.id = m3.parent_id
JOIN menu as m1 on m1.id = m2.parent_id
LEFT JOIN dbo.ParmsToList( '%" + keyword + "%', ',' ) f  ON 1 = 1


and here are some of the output


id p_name s_name value
579 Bass Broom 300 mm / 380 mm  Bass Broo %" + keyword + "%
580 Platform Broom STIFF With Wooden Handle Platform  %" + keyword + "%
581 Yard Broom With Handle Yard Broo %" + keyword + "%
652 Gutter Sweeper Stiff or Soft with Handles Gutter Sw %" + keyword + "%
725 Testing Testing %" + keyword + "%
814 HIGH SPEED DRILL BITS HIGH SPEE %" + keyword + "%
808 OPEN AND RING SPANNER OPEN AND  %" + keyword + "%
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800623
"keyword" should be the keyword you put ....
SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name , f.value
FROM  products p
JOIN menu as m3 ON m3.id = p.menu_id
JOIN menu as m2 on m2.id = m3.parent_id
JOIN menu as m1 on m1.id = m2.parent_id
LEFT JOIN dbo.ParmsToList( 'welding inverter', ',' ) f  ON 1 = 1  

Open in new window


hence:
SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name 
FROM  products p 
JOIN menu as m3 ON m3.id = p.menu_id 
JOIN menu as m2 on m2.id = m3.parent_id 
JOIN menu as m1 on m1.id = m2.parent_id 
JOIN dbo.ParmsToList( 'welding inverter', ',' ) f 
  ON m2.title LIKE '%' + f.value + '%' 
  OR m3.title LIKE '%' + f.value + '%'  
  OR m1.title LIKE '%' + f.value + '%'  
  OR p.p_name LIKE '%' + f.value + '%'  
  OR p.p_code LIKE '%' + f.value + '%'  
                                            

Open in new window

0
 

Author Comment

by:JCWEBHOST
ID: 38800642
sorry i am lost, i have used this query

SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name
FROM  products p
JOIN menu as m3 ON m3.id = p.menu_id
JOIN menu as m2 on m2.id = m3.parent_id
JOIN menu as m1 on m1.id = m2.parent_id
JOIN dbo.ParmsToList( 'welding inverter', ',' ) f
  ON m2.title LIKE '%' + f.value + '%'
  OR m3.title LIKE '%' + f.value + '%'  
  OR m1.title LIKE '%' + f.value + '%'  
  OR p.p_name LIKE '%' + f.value + '%'  
  OR p.p_code LIKE '%' + f.value + '%'


but no results found for welding inverter

but if i use welding or inverter single word it returns values

please help
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38800649
sorry, my fault. the "split" character is space, not ,

SELECT p.id, p_name, SUBSTRING(p.p_name, 0, 10) as s_name
FROM  products p
JOIN menu as m3 ON m3.id = p.menu_id
JOIN menu as m2 on m2.id = m3.parent_id
JOIN menu as m1 on m1.id = m2.parent_id
JOIN dbo.ParmsToList( 'welding,inverter', ',' ) f
  ON m2.title LIKE '%' + f.value + '%'
  OR m3.title LIKE '%' + f.value + '%'  
  OR m1.title LIKE '%' + f.value + '%'  
  OR p.p_name LIKE '%' + f.value + '%'  
  OR p.p_code LIKE '%' + f.value + '%'

Open in new window


should work
0
 

Author Closing Comment

by:JCWEBHOST
ID: 38800794
thanks
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question