Solved

sql like query

Posted on 2013-01-20
23
301 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 142

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
 

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 142

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 142

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 142

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 142

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 142

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 142

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 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now