Reputation: 289
I am working on an application in asp.net. There is a page to search employee details. User can enter any value to search in multiple fields like
FirstName, LastName, DateOfBirth, EmployeeCode
User can enter value like this:
Raj*,1980
That means user want to search Raj*
and 1980
among the number of fields given above.
I was looking at full-text search in SQL Server but the problem is that full-text search does not work for DateTime
columns.
The data can be coming from multiple tables.
Please give me any solution.
Upvotes: 0
Views: 497
Reputation: 515
Not sure, but it may help you.
Declare @Input as varchar(50) = 'Field1,Field2,Field3'
DEClare @Character as CHAR(1) = ','
DECLARE @StartIndex INT, @EndIndex INT
DECLARE @Output TABLE (ID int IDENTITY(1,1),
Item NVARCHAR(1000)
)
SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
BEGIN
SET @Input = @Input + @Character
END
WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)
INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)
SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END
--Finally Received all Search Keyword Values in @Output table
--Now Moving towards Search functionality
DECLARE @Result Table(data varchar(50),ID int)
WHILE (1=1)
BEGIN
IF(SELECT COUNT(1) FROM @Output) > 0
BEGIN
DECLARE @ID_OT INT = 0,@keyword AS VARCHAR(50) = ''
SELECT TOP 1 @ID_OT = ID,@keyword = Item FROM @Output ORDER BY id
--Here you need to identify a unique column which can be used to filter redundant values
INSERT INTO @Result
SELECT data,id
FROM TableName T
Left join @Result R ON T.id = R.id
WHERE
((FirstName like '%' + @keyword + '%') OR
(LastName like '%' + @keyword + '%') OR
(EmployeeCode like '%' + @keyword + '%') OR
(ISDATE(@keyword) = 1 AND CONVERT(VARCHAR(15),DateOfBirth,110) like '%' + CONVERT(VARCHAR(15),@keyword,110) + '%'))
AND R.id IS NULL
DELETE FROM @Output WHERE id = @ID_OT
END
ELSE
BREAk
END
Upvotes: 1
Reputation: 121649
Why can't you make a "mixed query" like this:
SELECT * FROM MyTable
WHERE CONTAINS(columnName, '"Toy Dog" OR "live animal"')
AND start_date > ###;
See also:
Upvotes: 0