Click here to Skip to main content
15,662,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm having trouble implementing a query for searching names on a table PERSON, which has two columns, FORENAME and SURNAME.

I'll use FULL TEXT SEARCH for doing that, and already have created the FULL TEXT CATALOG and the FULL TEXT INDEX on this table.

Supposing that i have the following rows on the table:
1. FORENAME: "Barack"     SURNAME: " of Mortal Kombat"
2. FORENAME: "Sadam"      SURNAME: "Houssein"
3. FORENAME: "Crystal"    SURNAME: "Obakwelu"

And i'm trying to search this name:

. Name to search: "Barack Hussain Obama"

I would do the sql query like this, supposing that @name is a parameter, and it would be passed by the user through the application layer, like a C# program:
declare @name nvarchar(30)
set @name = N'Barack Hussain Obama'

FROM Person

How can i get this working? The problem is in the @name string.
ERROR: Syntax error near 'Hussain' in the full-text search condition 'Barack Hussain Obama'.
Updated 21-Jan-13 7:47am
Richard C Bishop 21-Jan-13 12:46pm    
You have an "N" after the equal sign and before your string value when setting @name.

1 solution

The minimum required here, as long as you've managed to full-text index this table "PERSON", is:
DECLARE @name [nvarchar](30)
SET @name = 'Barack'

That being said, in order to search for this string:
SET @name N'Barack Hussain Obama'

You'd have to be concatenating two fields, ie-> [FORENAME] + [SURNAME], which would quickly make a simple search run through a table of first & last names quite a but more difficult. My solution, as begun, would entail searching through once, for either the FORE or SUR, getting all the results ... tabulating them, then searching again, after making THAT table FULL-TEXT INDEXED, for the second part. I don't see any real logic to trying to concatenate first and last then looping through another table of concatenations ...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900