title: Using SQL variable in like statement
body: <p>I have done much research on this but just cannot get it right. I am trying to query an audit trail using the below:</p>
  
  <pre><code>declare @batchid char (30)
  select @batchid='13857584'  --enter batch id here
  declare @occurred int 
  select @occurred='5'
  print 'Batch is in system, check MRN and encounter. User also noted below'
  select a.encounter, a.mrn, a.facility, c.fullname, t.action_desc, a.occurred, a.remark 
  from audit..audit_trail a (nolock)
  join cabinet..users c (nolock)
  on a.userinstanceid=c.userinstanceid
  join audit..action_table t (nolock)
  on a.action=t.action
  where a.REMARK like '%' +  @batchid  + '%'
  and OCCURRED &gt; GETDATE()-@occurred
  order by a.occurred
  </code></pre>
  
  <p>for some reason it will never return the results i want (no results). But when i run it specifically for the batch id like below (instead of @batchid)</p>
  
  <pre><code>declare @batchid char (30)
  select @batchid='13857584'  --enter batch id here
  declare @occurred int 
  select @occurred='5'
  print 'Batch is in system, check MRN and encounter. User also noted below'
  select a.encounter, a.mrn, a.facility, c.fullname, t.action_desc, a.occurred, a.remark 
  from audit..audit_trail a (nolock)
  join cabinet..users c (nolock)
  on a.userinstanceid=c.userinstanceid
  join audit..action_table t (nolock)
  on a.action=t.action
  where a.REMARK like '%' +  '13857584' + '%'
  and OCCURRED &gt; GETDATE()-@occurred
  order by a.occurred
  </code></pre>
  
  <p>it works perfectly. I tried adding quotes after and before percentage signs but i just cant get it right. Any help appreciated. SQL 2008</p>