Solved

Modifying this query so it searches based on search text?

Posted on 2011-09-23
10
205 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
Comment Utility
SELECT * FROM published_ads  
WHERE [email_address] like @searchText
    OR  title like @searchText
0
 
LVL 2

Author Comment

by:MyersA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 7

Expert Comment

by:mmr159
Comment Utility
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
Comment Utility

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
Comment Utility

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
Comment Utility
I ended up using a combination these two solutions.

Thanks again for all your help.
0
 
LVL 3

Expert Comment

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

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
back end of ssas cube views or tables? 2 24
encyps queries mssql 15 26
Sql query 34 17
How to simplify my SQL statement? 10 10
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

743 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

15 Experts available now in Live!

Get 1:1 Help Now