Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL How to ignore string parameter

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Andy Green

ASKER

Thanks again angelIII