Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2004-02-05 : 10:26:45
|
| Hello,I have a column in a database that contains a question. ex: "how do I restart the agent?". I have a stored procedure that accepts a set of keywords, as input, in this form: 'how','do','agent'.In the stored procedure, I need to take those keywords and see if any of those keywords exist in the question column. I thought of:SELECT * FROM TABLEWHERE Question IN ( 'how','do','agent' )This would work great if Question only consisted of one word. If the Question column had only 'agent' in it, then it would see it as a match. But the question column has many words. Consider the following pseudocode:'agent' IN 'how','do','agent' ... is a match BUT'how do I restart the agent?' IN 'how','do','agent' ... is not a match, even though some words are found in bothI hope this makes sense. I am not asking someone to write this code for me, I just have no clue how to do this in ONE select statement without using loops.Thanks!!!! |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-02-05 : 10:41:00
|
| SELECT * FROM TABLE WHERE Question LIKE '%how%'OR Question LIKE '%do%'OR Question LIKE '%agent%' |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2004-02-05 : 10:44:38
|
Thanks for the reply. I know that works, but I was wondering if there is a way to do this without haivng to break apart the incoming string of 'how','do','agent'. Similar to:create procedure spGet(@Keywords varchar(250) /* contains a string like 'how','do','agent' */)ASSELECT * FROM Table1 WHERE Question IN ( @Keywords )My query would work if Question only contained agent, for example. Because this is a true statement:'agent' IN ('how', 'do', 'agent')This is not true:'how do agent' IN ('how', 'do', 'agent')Hope I am making sense. Thanks!quote: Originally posted by ditch SELECT * FROM TABLE WHERE Question LIKE '%how%'OR Question LIKE '%do%'OR Question LIKE '%agent%'
|
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-02-05 : 10:51:04
|
| This can probably be achieved making use of the charindex function, it is in books online |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2004-02-05 : 12:23:23
|
Ditch, thanks for your help. I did find that the CONTAINS full-text search function does what I needed in my scenario. Thanks again.quote: Originally posted by ditch This can probably be achieved making use of the charindex function, it is in books online
|
 |
|
|
|
|
|
|
|