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

x
?
Solved

SQL How to ignore string parameter

Posted on 2010-09-23
3
Medium Priority
?
393 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
[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 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

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.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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