Home  >  Article  >  Database  >  How to Convert `varbinary` to Character Data in MySQL?

How to Convert `varbinary` to Character Data in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-10-27 04:22:30985browse

How to Convert `varbinary` to Character Data in MySQL?

Converting varbinary to Character Data in MySQL

Are you struggling to convert data in a varbinary field to a character-based format (char/varchar) in MySQL? Here's how you can accomplish this in MySQL version 5.10:

Conversion Methods

To convert varbinary to char/varchar, you can utilize the CAST or CONVERT functions:

<code class="sql">CAST(foo AS CHAR(100))
CONVERT(foo, CHAR(100))</code>

Replace "foo" with the name of your varbinary field and "CHAR(100)" with the desired character data type and length.

Supported Data Types

MySQL supports casting varbinary to the following data types:

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

Limitations

Note that you cannot directly cast varbinary to varchar. This is due to an unresolved MySQL bug from 2008.

Example Usage

To convert the contents of the "data" field from varbinary to CHAR with a maximum length of 100 characters, use the following query:

<code class="sql">UPDATE my_table SET data = CAST(data AS CHAR(100));</code>

The above is the detailed content of How to Convert `varbinary` to Character Data in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn