title: Select Top 1 field and assign to local variable
body: <p>I want to take the value of ExtractedDate from this query and use it as  @LastExtractDate in the next query.
  How do I do that?</p>
  
  <pre><code>    SELECT TOP 1 [ExtractedDate]
      FROM [OnsiteV4].[dbo].[SqlPendingIndex] order by ExtractedDate desc
  </code></pre>
  
  <p>next query:</p>
  
  <pre><code>    insert into @table(Hex, KeyDeviceId, ObjectDateTime, ExtractedDate  )
  SELECT     CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), ObjectValue, 1)) AS Hex, KeyDeviceId, ObjectDateTime , GETDATE ()
      FROM         SQLPending
      WHERE     (ObjectSubType LIKE '%GAS%') and (ObjectDateTime &gt; @LastExtractDate)
  </code></pre>