Solved

Modifying this query so it searches based on search text?

Posted on 2011-09-23
10
206 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
 
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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

863 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

26 Experts available now in Live!

Get 1:1 Help Now