Solved

# Selective Where statement

Posted on 2011-09-30
Medium Priority
233 Views
In my "where" section that's attached I need to use ONLY @searchName if len(@searchName) > 0

Otherwise use the @firstName or @lastName and NOT @searchName at all
``````Where	l.last_name Like '%' + IsNull(@lastName, l.last_name) + '%'
or
l.first_name Like '%' + IsNull(@firstName, l.first_name) + '%'
or
p.name like '%' + isnull(@searchName, p.name) + '%'
``````
0
Question by:lrbrister
[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

LVL 21

Expert Comment

ID: 36891487
You can use a case statement to create a conditional where clause.  Try this.

Greg

``````Where	l.last_name Like '%' + IsNull(@lastName, l.last_name) + '%'
or
l.first_name Like '%' + IsNull(@firstName, l.first_name) + '%'
or
--p.name like '%' + isnull(@searchName, p.name) + '%'
p.name LIKE CASE WHEN LEN(@searchName) = 0 THEN p.name ELSE '%' + @searchName + '%' END
``````
0

LVL 15

Accepted Solution

tim_cs earned 2000 total points
ID: 36891506
WHERE
((l.last_name Like '%' + IsNull(@lastName, l.last_name) + '%' OR l.first_name Like '%' + IsNull(@firstName, l.first_name) + '%') AND LEN(@searchName) = 0)
OR (p.name like '%' + isnull(@searchName, p.name) + '%' AND LEN(@searchName) > 0)

0

LVL 18

Expert Comment

ID: 36891507
Where (len(@searchName) > 0 AND p.name like '%' + isnull(@searchName, p.name) + '%')
OR
(len(@searchName) = 0 AND l.last_name Like '%' + IsNull(@lastName, l.last_name) + '%')
or
(len(@searchName) = 0 AND l.first_name Like '%' + IsNull(@firstName, l.first_name) + '%')

0

Author Closing Comment

ID: 36891606
This is final solution with actual valid data returned.
Thanks

WHERE
((l.last_name Like '%' + IsNull(@lastName, l.last_name) + '%' and l.first_name Like '%' + IsNull(@firstName, l.first_name) + '%') AND LEN(@searchName) = 0)
OR (p.name like '%' + isnull(@searchName, p.name) + '%' AND LEN(@searchName) > 0)
0

LVL 9

Expert Comment

ID: 36891654
If you want to conditional statement in where clause, Dynamic Sql is the only option available.
0

## Featured Post

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
###### Suggested Courses
Course of the Month10 days, 20 hours left to enroll