Solved

Conversion of Varbinary to nvarchar in SQL Server.

Posted on 2011-09-26
4
1,668 Views
Last Modified: 2012-10-31
My requirement is to write a Transact SQL function which can read the binary data in a VarBinary(Max) column, then convert it into nVarChar(Max)(The data in the VarBinary(Max) is created using a java application with utf-8 encoding.).

When I use the cast keyword to convert the Varbinary to nVarchar, all ascii characters are converted properly, but the unicode characters (Ex:Japanese) are not converted correctly.

Please provide a solution for this.
0
Comment
Question by:anusankarpn
  • 2
4 Comments
 
LVL 3

Expert Comment

by:anjos
ID: 36597869
"Web application may have inserted formatting information which may not be compatible with CONVERT.
In a varbinary(max) you are free to do anything. You don't have to stick to 2 bytes (unicode size) boundary. If the characters are shifted from the boundary, then plain CONVERT cannot read it back correctly. On the other hand, the webapp can read it back because it knows the format. "

Would it not be possible to read the binary data back through the same application that generated it?
0
 

Author Comment

by:anusankarpn
ID: 36597908
Currently our unicode data is stored in VarBinary(Max) column. We are trying to Drop this column and store the data in a new nVarChar(max) column.

We were trying to to do this conversion in The DB it self, rather than fetching the entire data to the application, convert it and then store it back to the new column.

We were able to find a solution in Oracle. But in SQL Server, we are not sure how to specify the encoding while converting. Is there any Stored procedures which can do this?

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36597910
as above comment, convert cannot know "application-specific rules" on how to interprete the data.
unless you create your own convertion function in t-sql, and use that function instead, I see no other way to do it, but surely not with t-sql convert. ...
0
 
LVL 3

Accepted Solution

by:
anjos earned 500 total points
ID: 36598006
Sql help docs state the following:
"Because Unicode data always uses an even number of bytes, use caution when converting binary or varbinary to or from Unicode supported data types. For example, this conversion does not return a hexadecimal value of 41, but of 4100:  SELECT CAST(CAST(0x41 AS nvarchar) AS varbinary).

When data is converted from a string data type (char, varchar, nchar, nvarchar, binary, varbinary, text, ntext, or image) to a binary or varbinary data type of unequal length, Microsoft® SQL Server™ pads or truncates the data on the right. When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is done with hexadecimal zeros."

"When the output of CAST or CONVERT is a character string, and the input is a character string, the output has the same collation and collation label as the input. If the input is not a character string, the output has the default collation of the database, and a collation label of coercible-default.

To assign a different collation to the output, apply the COLLATE clause to the result expression of the CAST or CONVERT function. For example:

SELECT CAST('abc' AS varchar(5)) COLLATE French_CS_AS"

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question