[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Assemble Full Text Catalog Search String

Posted on 2010-08-17
3
Medium Priority
?
247 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

650 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