29

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

Prasanna Raghu
  • 2,593
  • 3
  • 22
  • 25

3 Answers3

45

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

gbn
  • 422,506
  • 82
  • 585
  • 676
  • 1
    @gbn, What's the default encoding used for the `CHAR` in `convert`? Also, now that Oracle has taken over MySQL, are people putting more attention into fixing the bug? – Pacerier Jan 31 '15 at 09:07
  • 1
    @Pacerier: sorry, I'm not active with MySQL nowadays, – gbn Feb 03 '15 at 12:57
28

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

select cast(binaryColumn as CHAR) from table_name
yanigisawa
  • 731
  • 1
  • 8
  • 19
1

You can use cast operation:

select cast(column_name as char)
  from table_name
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292