title: How do I use a comma separated list of values as a filter in T-SQL?
body: <p>I have a basic SQL query, starting with:</p>
  
  <pre><code>SELECT top 20 application_id, [name], location_id FROM apps
  </code></pre>
  
  <p>Now, I would like to finish it so that it does this (written in Pseudocode)</p>
  
  <pre><code>if @lid &gt; 0 then
      WHERE location_id IN (@lid)
  else
      WHERE location_id is all values in location_id column
  </code></pre>
  
  <hr>
  
  <p>As requested, here is an example</p>
  
  <pre><code>application_id             name               location_id
  ----------------------------------------------------------
  1                          Joe Blogs          33
  2                          Sam Smith          234
  3                          Jeremy Carr        33
  </code></pre>
  
  <p>@locid is the results given by the user, for example '33, 234'</p>
  
  <p>If @lid is empty then I'd like it to output all rows for location_id with name and application_id. Otherwise, I'd like it to output all rows in relation to the provided numbers in @lid (standing for location_id.</p>
  
  <p>So, if @lid is 0:</p>
  
  <pre><code>application_id             name               location_id
  ----------------------------------------------------------
  1                          Joe Blogs          33
  2                          Sam Smith          234
  3                          Jeremy Carr        33
  </code></pre>
  
  <p>Otherwise, if @lid contains '33'</p>
  
  <pre><code>application_id             name               location_id
  ----------------------------------------------------------
  1                          Joe Blogs          33
  3                          Jeremy Carr        33
  </code></pre>