?
Solved

Assemble Full Text Catalog Search String

Posted on 2010-08-17
3
Medium Priority
?
246 Views
Last Modified: 2012-05-10
I have a field in a database that I need to assemble a string for (I'm not great with advanced string operations in t-sql).  

What the Field Contains:  John R. Smith Holding Company

The string I'd like to assemble for a full text catalog search:
"John" AND "Smith" AND "Holding" AND "Company"

Thanks!
0
Comment
Question by:naisnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Expert Comment

by:mcv22
ID: 33458740
You can use the following function

CREATE FUNCTION [dbo].[Split] (@separator VARCHAR(32), @string VARCHAR(MAX))

RETURNS @t TABLE
    (
        val VARCHAR(MAX)
    )  
AS
    BEGIN
        DECLARE @xml XML
        SET @XML = N'<root><r>' + REPLACE(@s, @sep, '</r><r>') + '</r></root>'

        INSERT INTO @t(val)
        SELECT r.value('.','VARCHAR(5)') as Item
        FROM @xml.nodes('//root/r') AS RECORDS(r)

        RETURN
    END

e.g. SELECT * FROM dbo.Split(' ', 'John R. Smith Holding Company')

You can store the results of the above into a temporary table. Remove any invalid characters using the REPLACE function and any invalid rows using DELETE and then combine them using something like this:

declare @Str varchar(1000)
set @Str = ''
select @Str = @Str + CASE WHEN LEN(@Str) = 0 THEN '"' ELSE '" AND "' END + ColumnName from TableName
set @Str = @Str + '"'
0
 
LVL 1

Accepted Solution

by:
geonon earned 2000 total points
ID: 33458798
u can search with this function:
e.g:

string word = "Holding" // this is an example

SELECT Field
FROM Table
WHERE Field like ("%"+word+"%") // the '%' is a jocker
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33460135
I could have sworn the author was asking a question related to the use of Full-Text Search and in particular the CONTAINS clause.  I guess I was mistaken.
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

770 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