title: How to compare varbinary data type in where clause
body: <p>I have a linked server that is created to pull user details from a specific Organisation Unit with a scheduled sql job agent.</p>
  
  <p>The table is created to hold user details has a column for ObjectGUID number and the type is defined as varbinary(50) (I am not sure why..).</p>
  
  <p>The process checks if there is a new user by comparing the ObjectGUID number the saved Users table and if there is a new number then insert the new user in the table.</p>
  
  <p>However I have noticed that the comparisons actually not really working properly. </p>
  
  <pre><code>SELECT 
  tbl.objectGUID AS UserGUID 
  FROM [dbo].[ActiveDirectoryUsers] tbl
  WHERE tbl.objectGUID NOT IN (SELECT UserGUID FROM dbo.Users)
  </code></pre>
  
  <p>When I create a new user the new user is appearing in the ActiveDirectoryUsers view.</p>
  
  <p>but when the where clause added to compare results with Users table then result is always empty. It looks like I need to cast or convert the varbinary to varchar then do the comparisons. I tried to cast the varbinary into varchar and uniqueidentifier but still it does not work.</p>
  
  <p>Any idea how would I do the comparisons? </p>
  
  <blockquote>
    <p>Update</p>
  </blockquote>
  
  <pre><code>CREATE VIEW [dbo].[ActiveDirectoryUsers] AS 
  SELECT "SAMAccountName" AS sAMAccountName, "mail" AS Email,
    "objectGUID" AS objectGUID
     FROM OpenQuery(ADSI, 'SELECT SAMAccountName, mail, objectGUID
                 FROM   ''ldapconnectionstring.com''')
  </code></pre>
  
  <p>An example of objectGUID in the Users table </p>
  
  <blockquote>
    <p>0x1DBCC071C69C8242B4895D42750969B1</p>
  </blockquote>