Andy Green
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(@Special ty)).
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
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(@Special
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again angelIII
SELECT * FROM TableName
WHERE (CASE WHEN ISNULL(@Specialty, '') = '' THEN 1
WHEN @Specialty IS NOT NULL AND Specialty IN (Select Value From dbo.fnParmsToList(@Special
ELSE 0) = 1