Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conversion of Varbinary to nvarchar in SQL Server.

Posted on 2011-09-26
4
Medium Priority
?
1,960 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 1500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

670 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