title: Decoding SQL Server Varbinary Datatype to a Java String
body: <p>Trying to bypass a Jboss Hibernate issue in Java where you can't handle Nvarchar SQL Server Datatypes from a stored procedure. I can't convert or cast the datatypes to varchar due to the fact we store multibyte characters, therefore, the datatype is a must. I am however, able to convert the type to varbinary in the stored procedure and decode in Java</p>
  
  <p>The SQL Server Proc:</p>
  
  <pre><code>Convert(varbinary, Tile)  AS Title
  </code></pre>
  
  <p>On the Java side, I read the result set and the field, now encoded such as</p>
  
  <blockquote>
    <p><strong>UPDATED to Actual encoded:</strong></p>
  </blockquote>
  
  <pre><code>0x440065007300690067006E0020002600200050006C0061006E006E006900 
  </code></pre>
  
  <p>I attempt to parse this bytearray into a string as follow:</p>
  
  <pre><code>            byte[] bytes = (byte[]) dataField[0]; 
  
              try {
  
                  String varbinaryString = new String(bytes, "UTF-8");
                  System.out.println(varbinaryString);
  
  
  
              } catch (UnsupportedEncodingException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  </code></pre>
  
  <p>The result is pretty much the intended decoded string from the Title field, only, it is decoded in this manner: </p>
  
  <pre><code>D e s i g n  &amp;  P l a n n i
  </code></pre>
  
  <p>rather than the straight out put of </p>
  
  <pre><code>Design &amp; Planning Director
  </code></pre>
  
  <p>Can anyone assist me in the dilemma with Java decoding a SQL Server varbinary datatype?</p>
  
  <p>Thank you in advance for your help or hints. </p>