Solved

Modifying this query so it searches based on search text?

Posted on 2011-09-23
10
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 15

Accepted Solution

by:
tim_cs earned 250 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 250 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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 42
SSIS GUID Variable 2 31
Open A Form without Loading All the Records of Its Recordsource 6 44
Regarding Disk IO 3 42
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

740 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