Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Modifying this query so it searches based on search text?

Posted on 2011-09-23
10
Medium Priority
?
214 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 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