title: Join SQL Server tables on a like statement
body: <p>I am hoping this isn't a repeat. I've checked the searches and I can't seem to find a clear answer to this.</p>
  
  <p>I have a table that has it's primary key set to be a <code>UniqueIdentifier</code>. I also have another table that has a <code>varchar</code> column that basically contains a url with a query string that contains guids from my first table.  </p>
  
  <p>So my 2 tables are like:</p>
  
  <p><strong>StateTable</strong></p>
  
  <pre><code>StateID                                  StateName
  EB06F84C-15B9-4397-98AD-4A63DA2A238E     Active
  </code></pre>
  
  <p><strong>URLTable</strong></p>
  
  <pre><code>URL
  page.aspx?id=EB06F84C-15B9-4397-98AD-4A63DA2A238E
  </code></pre>
  
  <p>What I'm trying to do is join together <code>URLTable</code> and <code>StateTable</code> ON the value of <code>StateID</code> is contained in URL of URL table. I haven't really figured out the join. I've even tried just selecting the one table and tried to filter by the values in <code>StateTable</code>. I've tried doing something like this:</p>
  
  <pre><code>SELECT  *
  FROM URLTable
  WHERE     EXISTS
      (SELECT  *
       FROM  StateTable
       WHERE URL LIKE '%' + StateID + '%')
  </code></pre>
  
  <p>Even that doesn't work because it says I'm comparing <code>uniqueidentifier</code> and <code>varchar</code>. </p>
  
  <p>Is there any way to join 2 tables using a like command and where the like command isn't comparing 2 incompatible variables?</p>
  
  <p>Thank you!! </p>
  
  <p>UPDATE:  Let me add some additional things I should have mentioned. The query is for the purposes of building analytics reports. The tables are part of a CMS analytics package... so updating or changing the table structure is not an option.  </p>
  
  <p>Secondly, these tables see a very high amount of traffic since they're capturing site analytics... so performance is very much an issue. The 3rd thing is that in my example, I said id=  but there may be multiple values such as <code>id=guid&amp;user=guid&amp;date=date</code>. </p>
  
  <p>UPDATE 2: One more thing I just realized to my horror is that sometimes the query string has the dashes removed from the GUID.. and sometimes not.. so unless I"m mistaken, I can't cast the substring to <code>Uniqueidentifier</code>. Can anyone confirm? <em>sigh</em>. I did get it to work using </p>
  
  <pre><code>REPLACE('-','',CONVERT(varchar(50), a.AutomationStateId))
  </code></pre>
  
  <p>but now I'm very much worried about performance issues with this since the URL's table is very large. This might be the nature of the beast, though, unless there's anything I can do.</p>