title: SQL Server - Replacing Single Quotes and Using IN
body: <p>I am passing a comma-delimited list of values into a stored procedure. I need to execute a query to see if the ID of an entity is in the comma-delimited list. Unfortunately, I think I do not understand something. </p>
  
  <p>When I execute the following stored procedure:</p>
  
  <pre><code>exec dbo.myStoredProcedure @myFilter=N'1, 2, 3, 4'
  </code></pre>
  
  <p>I receive the following error:</p>
  
  <pre><code>"Conversion failed when converting the varchar value '1, 2, 3, 4' to data type int."
  </code></pre>
  
  <p>My stored procedure is fairly basic. It looks like this:</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[myStoredProcedure]
      @myFilter nvarchar(512) = NULL
  AS
  SET NOCOUNT ON
  BEGIN
      -- Remove the quote marks so the filter will work with the "IN" statement
      SELECT @myFilter = REPLACE(@myFilter, '''', '')
  
      -- Execute the query
      SELECT
          t.ID,
          t.Name      
      FROM
          MyTable t
      WHERE
          t.ID IN (@myFilter)
      ORDER BY
          t.Name
  END
  </code></pre>
  
  <p>How do I use a parameter in a SQL statement as described above? Thank you!</p>