Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from varbinary to char/varchar in mysql

Tags:

sql

mysql

I have a field which is varbinary. It has already been populated. Now how do i convert varbinary to varchar so that I can use the data in the field for some other purpose. I use a MySQL version 5.10

like image 828
Prasanna Raghu Avatar asked Dec 09 '09 10:12

Prasanna Raghu


People also ask

What is VARBINARY in MySQL?

The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings. M represents the maximum column length in bytes. It contains no character set, and comparison and sorting are based on the numeric value of the bytes.

What is CAST () in MySQL?

The MySQL CAST() function is used for converting a value from one datatype to another specific datatype. The CAST() function accepts two parameters which are the value to be converted and the datatype to which the value needs to be converted.

Can we convert varchar to VARBINARY in SQL Server?

Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query. The problem is that the query plan hashes are already in binary format, however stored as VARCHAR in the XML Query Plan e.g.

What does VARBINARY mean in SQL?

VarBinary is a variable width data type. The syntax for declaring Binary variable is varbinary(n) , where n defines the maximum size in bytes. The varbinary data type uses actual length of the data entered + 2 bytes as the storage.


2 Answers

Late answer...

You can use CAST or CONVERT thus

CAST(foo AS CHAR(100)) CONVERT(foo, CHAR(100)) 

Supported types (5.5) are:

BINARY[(N)] CHAR[(N)] DATE DATETIME DECIMAL[(M[,D])] SIGNED [INTEGER] TIME UNSIGNED [INTEGER] 

You can not cast to varchar directly.
There is an open MySQL bug from 2008 which no-one seems to care about and is damn annoying

like image 179
gbn Avatar answered Sep 30 '22 23:09

gbn


The MySQL syntax that worked for me in a similar scenario to this is:

select cast(binaryColumn as CHAR) from table_name 
like image 41
yanigisawa Avatar answered Sep 30 '22 22:09

yanigisawa