title: How to conditionally exclude or add columns when calculating row total
body: <p>I have a stored procedure with a parameter defined like below</p>
  
  <pre><code>    @CategoryNames  NVARCHAR(MAX)
  </code></pre>
  
  <p>The above parameter gets a comma separated string from the application, for example the value can be like this <code>@StaffCategoryIds=N'Category1,Category2,Category3,Category4,Category5'</code></p>
  
  <p>Then i have the sample below sql query </p>
  
  <pre><code>SET @sql = @sql + 'TypeId, 
          TypeName, 
          Condition,
          Region, 
          (Category1 + Category2 + Category3 + Category4 + Category5) AS Total INTO ##QueryResults
  FROM Table1;'
  </code></pre>
  
  <p>When computing my Total column in the query above, how can i first check whether a category column (say Category2 or Category5) exists in my <code>@StaffCategoryIds</code> comma separated string before including it in the computation of my Total column?</p>
  
  <p>Eg. if 'Category2' exists in <code>@StaffCategoryIds</code>, then i include it in computation of Total else i exclude it.</p>