SQL Server 2008中列的多个搜索条件

时间:2022-04-20 01:35:42

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

我正在使用asp.net中的应用程序。有一个页面可以搜索员工详细信息。用户可以输入任何值来搜索多个字段,例如

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.

这意味着用户想要在上面给出的字段数中搜索Raj *和1980。

I was looking at full-text search in SQL Server but the problem is that full-text search does not work for DateTime columns.

我在SQL Server中查看全文搜索,但问题是全文搜索不适用于DateTime列。

The data can be coming from multiple tables.

数据可以来自多个表。

Please give me any solution.

请给我任何解决方案。

2 个解决方案

#1


0  

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:

#2


1  

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

#1


0  

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:

#2


1  

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