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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Find string in any field of several tables

Author  Topic 

sbt1
Yak Posting Veteran

89 Posts

Posted - 2005-09-09 : 14:30:36
I'm implementing a search; I'd like to find a way to write a query find a search string in any field of a table, instead of writing it like this:

... WHERE [Field1] LIKE %searchstring%
OR [Field2] LIKE %searchstring%
OR [Field3] LIKE %searchstring%
OR [Field4] LIKE %searchstring%

Is there a way to do this?

How about in multiple tables?

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2005-09-09 : 22:45:55
I'm not sure if this will be any faster or easier, but it's at least an alternative:

SELECT * FROM
(SELECT field1 AS col FROM YourTable
UNION
SELECT field2 AS col FROM YourTable
UNION
SELECT field3 AS col FROM YourTable
UNION
SELECT field4 AS col FROM YourTable) AS A
WHERE A.col LIKE '%searchstring%'

You could also make the derived table into a view, temp table or table variable.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-10 : 00:34:47
Silmilar to this one
http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-09-10 : 07:30:22
i use this one:
http://weblogs.sqlteam.com/brettk/archive/2005/02/08/4144.aspx

i put it in master databse and modified the sproc so i can put a dbname as an argument also.


Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -