Solved

SQL How to ignore string parameter

Posted on 2010-09-23
3
352 Views
Last Modified: 2012-05-10
Hi Experts

I have a select statement, where I pass in a parameter (@Specialty) this comes in as a comma separated list of values and I convert these to a table variable and use it like so.

Where Specialty in (Select Value From dbo.fnParmsToList(@Specialty)).

What I want is to ignore this value if it is null, (or empty string I can force it to either value in the calling code)

I usually use coalesce to return the first non null value, and this works great for single parameter values, but I cant get it to work with the above.

So my question is how can I ignore the parameter @Specialty and return ALL values unless I specify the parameter.

If I might just add, this is not the only value in the where clause, there are 5 others, where I use coalesce in 3.

Andy
0
Comment
Question by:Andy Green
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33741876
what about:

Where ( @Specialty IS NULL OR Specialty in (Select Value From dbo.fnParmsToList( @Specialty)) )

Open in new window

0
 

Expert Comment

by:madhav_ghatole
ID: 33741910
try this query

SELECT * FROM TableName
      WHERE (CASE WHEN ISNULL(@Specialty, '') = '' THEN 1
                    WHEN @Specialty IS NOT NULL AND Specialty IN (Select Value From dbo.fnParmsToList(@Specialty))
                    ELSE 0) = 1
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 33742026
Thanks again angelIII
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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard 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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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

11 Experts available now in Live!

Get 1:1 Help Now