Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Search in multiple fields

Author  Topic 

germallon
Starting Member

1 Post

Posted - 2009-06-05 : 09:49:06
Hello all,

I have a table with file information (path, filename, extension, size, etc), and I would like to enable search on this table based on different fields with the single input, and concatenation of the same. For example..

Say I have the following record in my DB:

Path: /home/media/mymusic
filename: rock_my_world
extension: mp3

I want to be able to find this file by typing any of the following in the user interface that I'll provide.


?> rock
?> rock_my_world
?> mp3
?> mymusic
?> rock_my_world.mp3
?> /home/media/mymusic/rock_my_world.mp3

I am doing something along the lines of: (where %s is the search term entered by the user)

SELECT basename, extension FROM tb_file WHERE (basename LIKE "%s") OR (extension LIKE "%s") OR (path LIKE "%s");

The above query takes care of the first 4 cases, but not the last two. Any suggestions?? Thank you in advance.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-06-05 : 10:24:06
this seems to work:

declare @s varchar(200)
--set @s = 'rock'
--set @s = 'rock_my_world'
--set @s = 'mp3'
--set @s = 'mymusic'
--set @s = 'rock_my_world.mp3'
set @s = '/home/media/mymusic/rock_my_world.mp3'

select d.*
from (
select [Path] = '/home/media/mymusic'
,[filename] = 'rock_my_world'
,extension = 'mp3'
) d
where charindex(@s, [Path] + '/' + [filename] + '.' + extension) > 0


Be One with the Optimizer
TG
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2009-06-05 : 10:25:15
Sounds like a candidate for a fuzzy-search algorithm:
http://sqlblindman.pastebin.com/f4fc1ccb5

One option would be to concatenate all the columns using this function:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MatchText]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[MatchText]
GO

CREATE function MatchText (@InputString varchar (50))
returns varchar (50)

begin
-- blindman, 7/2005

-- --test parameters
-- declare @InputString varchar(50)
-- set @InputString = 'Bruce a. Lindman'

declare @TempString varchar (50)
declare @OutputString varchar (50)
declare @CharNum integer
declare @TestChar CHAR(1)

--Convert to uppercase and remove noise characters
set @TempString = UPPER(@InputString)
set @TempString = replace(@TempString, 'A', '')
set @TempString = replace(@TempString, 'E', '')
set @TempString = replace(@TempString, 'I', '')
set @TempString = replace(@TempString, 'O', '')
set @TempString = replace(@TempString, 'U', '')

--Build @OutputString with only alphanumeric characters
set @CharNum = 1
set @OutputString = ''
while @CharNum <= len(@TempString)
begin
set @TestChar = substring(@TempString, @CharNum, 1)
if (@TestChar between 'A' and 'Z') OR (@TestChar between '0' and '9') set @OutputString = @OutputString + @TestChar
set @CharNum = @CharNum + 1
end

return @OutputString
end
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CompareText]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[CompareText]
GO

...and then apply the same function to the search string before running the CompareText function.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page
   

- Advertisement -