1

I have a Parameter called @Codes passing in an optional comma delimited string of system codes.

If this parameter has a value I want to filter the query with a

WHERE 
  blah = blah AND
  Code IN ('Code1','Code2','etc.')

If the parameter is null or empty I want to omit the last where filter entirely.

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stewart Alan
  • 1,521
  • 5
  • 23
  • 45

1 Answers1

4
WHERE 
  blah = blah AND
  (
  Code IN (SELECT TheItem FROM dbo.fnSplit(@Codes))
  OR
  @Codes IS NULL
  )

To change the CSV into a recordset, fee free to choose one of the many functions. Personally, I tend to the a numbers table approach

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676