1

I have a UDF that takes a space-delimited string and returns a table containing rows the individual strings. Something like this:

SplitTextReturnTable('Emmanuel John Ahmed', ' ') 

// returns a table containing three rows having Emmanuel, John and Ahmed.

What I want to do is use each of the returned string to perform a search in a table to retrieve rows with data matching the strings, something similar to a for-each-statement that searches the table with each of the returned strings. Here is a pseudo code of what I want.

DECLARE @myArray ARRAY;
SET @myArray = ConvertToArray(SplitTextReturnTable('Emmanuel John Ahmed', ' '))
SELECT * FROM Customers WHERE Customers.Fullname LIKE + '%' + @myArray[0] + '%' or Customers.Fullname LIKE + '%' + @myArray[1] + '%' or Customers.Fullname LIKE + '%' + @myArray[2] + '%'

Any help would be appreciated. I would also like to know if there's a better way to perform database searching more effectively.

Thanks.

1
  • I see you have edited to say that your function does actually return 3 rows. In that case you just need to join as in my answer. Commented Jan 20, 2011 at 12:22

1 Answer 1

3

Why does your split function only return one row for this input? It doesn't seem to be splitting at all! What you probably want it to do is to fix it to return a table with 3 rows that you can then join on. i.e. something like

SELECT c.* 
FROM Customers c
JOIN dbo.SplitTextReturnTable('Emmanuel John Ahmed', ' ') s
ON c.Fullname LIKE '%' + s.value + '%'

Regarding the second part of your question you might want to investigate Full Text Search. LIKE with leading wildcards is inherently slow.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Martin.Is there any way to use FullTextSearch with SQL SERVER 2005 EXPRESS Edition? Any link to resources and tutorials would be appreciated. Thanks
@Tobechukwu - Yes you can. You need to download the version with Advanced Services though. It doesn't come bundled with the other edition. I don't know of any particularly good resources in this area but its quite easy to set up full text indexing via a wizard and google should have plenty of stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.