Monday, January 21, 2013 11:01 PM
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'
select FORENAME, SURNAME
WHERE CONTAINS( (FORENAME, SURNAME), @name )
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'.
- Moved by Naomi NMicrosoft Community Contributor Monday, January 21, 2013 11:48 PM Better forum
Wednesday, January 23, 2013 8:26 AMModerator
Please try the following codes
select FORENAME, SURNAME FROM Person WHERE CONTAINS( FORENAME, 'Barack') and CONTAINS(SURNAME, 'Hussain' )
TechNet Community Support
- Marked As Answer by Allen Li - MSFTModerator Wednesday, January 30, 2013 2:11 AM
Monday, January 28, 2013 4:20 PM
If you are searching an exact phrase, you need double-quotes inside the single quote.
declare @name nvarchar(30) set @name = N'"Barack Hussain Obama"'
However, a full text search only looks at one column at a time. So, even with the search syntax above, the search would fail because the forename and surname are in different columns. Thus Allen Li's suggestion to search the forename in the FORENAME column and the surname in the SURNAME column.
Also, you can create a computed column that covered forename and surname. E.g.
CREATE TABLE dbo.PersonNames (FORENAME NVARCHAR(100), SURNAME NVARCHAR(100), COMBINEDNAME AS FORENAME + ' ' + SURNAME);
In that case, you would run your query against COMBINEDNAME (or *) and it would fine the combination. (Assuming, of course, that the full name was present and spelled consistently.)