Home > Database > Mysql Tutorial > body text

How to Convert VARBINARY Data to CHAR/VARCHAR in MySQL?

Susan Sarandon
Release: 2024-10-26 15:15:03
Original
447 people have browsed it

How to Convert VARBINARY Data to CHAR/VARCHAR in MySQL?

Converting Varbinary to Char/Varchar in MySQL

Problem:

You have a field in your MySQL database that is currently stored as varbinary, and you need to convert its data to varchar or char format for further processing.

Solution:

MySQL supports the conversion of varbinary data to char or varchar using the CAST or CONVERT functions.

CAST and CONVERT Syntax:

<code class="sql">CAST(varbinary_column AS char/varchar(length))
CONVERT(varbinary_column, char/varchar(length))</code>
Copy after login

where:

  • varbinary_column is the name of the varbinary field you want to convert.
  • char/varchar(length) specifies the desired character type and length.

Example:

To convert a varbinary field named binary_data to a character string that is 100 characters long, you would use:

<code class="sql">CAST(binary_data AS CHAR(100))</code>
Copy after login

or:

<code class="sql">CONVERT(binary_data, CHAR(100))</code>
Copy after login

Supported Types:

The following types can be converted to and from varbinary using CAST or CONVERT:

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

Note:

It is important to note that MySQL does not support direct casting from varbinary to varchar. There is an open bug report regarding this issue, and it remains unresolved as of MySQL version 5.5.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!