Solved

Conversion of Varbinary to nvarchar in SQL Server.

Posted on 2011-09-26
4
1,718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server sp_executesql / Change Tracking Version() results into variable 4 30
Search Text in Views 2 28
SQL Server Pivot 5 44
T-SQL Query 5 25
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

710 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