Solved

Assemble Full Text Catalog Search String

Posted on 2010-08-17
3
240 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
3 Comments
 
LVL 12

Expert Comment

by:mcv22
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 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

14 Experts available now in Live!

Get 1:1 Help Now