Solved

Selective Where statement

Posted on 2011-09-30
5
221 Views
Last Modified: 2012-05-12
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) + '%'

Open in new window

0
Comment
Question by:lrbrister
5 Comments
 
LVL 21

Expert Comment

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

Open in new window

0
 
LVL 15

Accepted Solution

by:
tim_cs earned 500 total points
Comment Utility
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

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

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

by:sachinpatil10d
Comment Utility
If you want to conditional statement in where clause, Dynamic Sql is the only option available.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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

10 Experts available now in Live!

Get 1:1 Help Now