title: Left Join with OR operator precedence
body: <p>I have the following query:</p>
  
  <pre><code>SELECT * FROM 
  dbo.Driver d
      LEFT JOIN #Test t ON  
         ((d.LicenseNumber = REPLACE(REPLACE(t.[LicNumber],'-',''),' ','')) 
          OR 
         (d.[BirthDate] = t.[BirthDate]
          AND  d.[LastName] = t.[LastName]
          AND d.[FirstName] = t.[FirstName])
          OR
         (SUBSTRING(d.[LicenseNumber], PATINDEX('%[^0]%', d.[LicenseNumber] + '.'), LEN(d.[LicenseNumber]))) = t.[LicNoZero])
  </code></pre>
  
  <p>What I am trying to figure out are a couple of things. </p>
  
  <p>First what I had before was this pseudo code:</p>
  
  <pre><code>IF Driver.License = #Test.DriverLicenseNumber(No Space/Dash) THEN JOIN
  ELSE
  IF Driver.BirthDate = #Test.BirthDate
  AND Driver.LastName = #test.LastName
  AND Driver.FirstName = #Test.FirstName THEN JOIN
  ELSE
  IF Driver.LicenseNoZero = #Test.LicNoZero THEN JOIN
  ELSE
  DO NOTHING
  </code></pre>
  
  <p>I am trying to convert this psuedo code to the SQL up above. At first I had a scalar function that had the IF's in it but that turned out to be very slow. </p>
  
  <p>Right now, the above left join is working, and is much faster, but I am wondering what the precedence of the statements are in the Left Join?</p>
  
  <p>Also, If 1  OR statement is true, and another OR statement is true, and they are true on different records in the #Test table, what happens?</p>
  
  <p>Is there any better alternatives to this as well?</p>
  
  <p>EDIT:
  The Tables are as follows:</p>
  
  <pre><code>Driver
  LicenseNumber, Birthdate,LastName,FirstName
  
  #Test
  Licnumber,Birthdate,LastName,FirstName,LicNoZero,Address
  </code></pre>
  
  <p>Basically what I am trying to do is pull the Address from the #Test table.</p>