Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Modifying this query so it searches based on search text?

Posted on 2011-09-23
10
Medium Priority
?
219 Views
Last Modified: 2012-05-12
I have two unrelated tables:
published_ads (with ID, title, email_address)
email_addresses (with ID, email_address)

I have a stored procedure with one parameter (@searchText) and this parameter is used for the following (oversimplified) query:

SELECT * FROM published_ads  WHERE [title] like @searchText

This is what I would like to do:
if the string in @searchText is a record of table [email_addresses], then the query will search in [published_ads.email_address]. Otherwise, the query will search [published_ads.title].

In other words, let's say @searchText is 'myEmail@email.com' and this string is in table [email_addresses]. Then the query will look like:
SELECT * FROM published_ads  WHERE [email_address] like '%myEmail@email.com%'

On the other hand, @searchText is 'shoes', which is not in table [email_addresses]. Then the query will look like:
SELECT * FROM published_ads WHERE [title] like '%shoes%'

Is this possible?

Thanks.
0
Comment
Question by:MyersA
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 36587935
SELECT * FROM published_ads  
WHERE [email_address] like @searchText
    OR  title like @searchText
0
 
LVL 2

Author Comment

by:MyersA
ID: 36588031
Well, I had that coming.

The problem there is that if the title has the email address as text, the the record will be displayed even if the email address isn't actually that string.

So if someone adds a record whose [published_ads.title] is 'sales@walmart.com' and its [published_ads.email_address] is 'I_Got_You@sucker.com' then this record will be displayed even though the record is clearly not from 'sales@walmart.com'.

That's why I don't want to use that solution.
0
 
LVL 7

Expert Comment

by:mmr159
ID: 36588058
I suppose taking the cheap way with dynamic SQL is out of the question?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
tim_cs earned 1000 total points
ID: 36588185
Maybe this?
DECLARE @EmailExists bit

SELECT @EmailExists = CASE WHEN EXISTS (SELECT * FROM email_addersses WHERE email_address like @searchText) THEN 1 ELSE 0 END

SELECT
	*
FROM	
	published_ads
WHERE
	(email_address like @searchText AND @EmailExists = 1)
	OR (title like @searchText AND @emailExists = 0)

Open in new window

0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 1000 total points
ID: 36588209
Perhaps something like this:

if @searchText LIKE '%@%'
      SELECT * FROM published_ads WHERE [email_address] like @searchText
else
      SELECT * FROM published_ads WHERE [title] like @searchText
0
 
LVL 7

Expert Comment

by:mmr159
ID: 36588476
If you go with wdosanjos method

> if @searchText LIKE '%@%'

use this instead

if CHARINDEX('@',@searchText) > 0

or PATINDEX

They provide better performance... probably negligible in this case, but why not.
0
 
LVL 3

Expert Comment

by:jvejskrab
ID: 36589229

IF EXISTS (SELECT 1 FROM email_addresses WHERE email_address = '%'+@searchText+'%')
    SELECT * FROM published_ads  WHERE [email_address] like '%'+@searchText+'%'
ELSE
    SELECT * FROM published_ads  WHERE title like '%'+@searchText+'%'


 
0
 
LVL 3

Expert Comment

by:jvejskrab
ID: 36589277

You can also have it in one query....


SELECT *
      FROM published_ads  
      WHERE CASE
                        WHEN CHARINDEX('@',@searchText) > 0  THEN [email_address]
                        ELSE [title]
                   END  like '%'+@searchText+'%'

or if an occurence of '@' char isn't enough, you can write some email validation function and use it within the query. Someting like this


SELECT *
      FROM published_ads  
      WHERE CASE
                        WHEN dbo.IsValidEmail(@searchText) = 1  THEN [email_address]
                        ELSE [title]
                   END  like '%'+@searchText+'%'
0
 
LVL 2

Author Closing Comment

by:MyersA
ID: 36589320
I ended up using a combination these two solutions.

Thanks again for all your help.
0
 
LVL 3

Expert Comment

by:jvejskrab
ID: 36589384
MyersA: Can you send how the final query looks like? Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

963 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