Solved

Conversion of Varbinary to nvarchar in SQL Server.

Posted on 2011-09-26
4
1,499 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
Comment Utility
"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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article I will describe the Backup & Restore 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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now