title: How can I print a binary value as hex in TSQL?
body: <p>I'm using SQL Server 2000 to print out some values from a table using <code>PRINT</code>.  With most non-string data, I can cast to nvarchar to be able to print it, but binary values attempt to convert using the bit representation of characters.  For example:</p>
  
  <pre><code>DECLARE @binvalue binary(4)
  SET @binvalue = 0x12345678
  PRINT CAST(@binvalue AS nvarchar)
  </code></pre>
  
  <p>Expected:</p>
  
  <blockquote>
    <p>0x12345678</p>
  </blockquote>
  
  <p>Instead, it prints two gibberish characters.</p>
  
  <p>How can I print the value of binary data?  Is there a built-in or do I need to roll my own?</p>
  
  <p>Update: This isn't the only value on the line, so I can't just PRINT @binvalue.  It's something more like PRINT N'other stuff' + ???? + N'more stuff'.  Not sure if that makes a difference: I didn't try just PRINT @binvalue by itself.</p>