Solved

General SQL Query Question

Posted on 1998-12-09
2
158 Views
Last Modified: 2010-03-19
How does one pass a LIKE query to all the fields in a database?  For example, the following:
     SELECT * from Genetics WHERE Genetics.author LIKE '%smith%'
Returns records with "smith" in the authors field, but what if I wanted to search for "smith" in ALL the fields?
I tried
     SELECT * from Genetics WHERE Genetics.* LIKE '%smith%'
And it didn't work (yes, I'm new to SQL).

Any help would be appreciated.

Thanks,

Dave Stephens

0
Comment
Question by:stepherd
2 Comments
 
LVL 2

Accepted Solution

by:
tschill120198 earned 150 total points
ID: 1092184
I would be surprised if there was a way to do it with just a single line of code...  it could be done using UNION.

Suppose the Genetics table had columns "author", "title", "description".  You could use something like the following:

select * Genetics where author like '%smith%'
union
select * Genetics where title like '%smith%'
union
select * Genetics where description like '%smith%'

If you don't know in advance what the columns are you could build the SQL dynamically.


0
 

Author Comment

by:stepherd
ID: 1092185
Thanks for the input. Yeah, I thought this might be the case.  I am building the SQL dynamically using PERL, so it shouldn't be a problem to approach it as you suggest.
Never hurts to ask! Thanks.

--Dave Stephens

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now