Home > Database > Mysql Tutorial > How to convert specified value to string type in mysql

How to convert specified value to string type in mysql

青灯夜游
Release: 2021-12-06 12:10:54
Original
36470 people have browsed it

Conversion method: 1. Use the CAST() function to convert the value to the specified data type. The syntax is "SELECT CAST (value to be converted AS string type);"; 2. Use CONVERT() Function, syntax "SELECT CONVERT (value to be converted, string type);".

How to convert specified value to string type in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

mysql converts the specified value into a string type

1. Use the CAST() function

CAST() function converts a value (of any type) to a specified data type.

Syntax

CAST(value AS datatype)
Copy after login
ParametersDescription
value Required. Field name or value to be converted
datatypeRequired. The data type to convert to. Can be one of the following:
  • DATE: Convert value to date. Format: "YYYY-MM-DD"

  • DATETIME: Convert value to DATETIME. Format: "YYYY-MM-DD HH:MM:SS"

  • TIME: The conversion value is time. Format: "HH:MM:SS"

  • CHAR: Convert value CHAR (fixed length string)

  • SIGNED: Convert value signature (Signed 64-bit integer)

  • UNSIGNED: Convert value to UNSIGNED (Unsigned 64-bit integer)

  • BINARY: Convert value to BINARY (binary string)

Example:

SELECT CAST(150 AS CHAR);
Copy after login

How to convert specified value to string type in mysql

2. Use the CONVERT() function

CONVERT() function to convert the value to the specified data type or character set.

Syntax

CONVERT(value, type)
Copy after login

Either:

CONVERT(value USING charset)
Copy after login
##ParametersDescriptionRequired. Value to convertcharset

示例:

SELECT CONVERT(150, CHAR);
Copy after login

How to convert specified value to string type in mysql

扩展知识:MySQL字符串类型

value
typeRequired. The data type to convert to. Can be one of the following:

  • #DATE: Convert value to date. Format: "YYYY-MM-DD"

  • ##DATETIME: Convert value to DATETIME.Format: "YYYY-MM-DD HH:MM:SS"
  • TIME: It’s time to convert value. Format: "HH:MM:SS"
  • CHAR: Convert value CHAR (fixed length string)
  • SIGNED: Convert value signature (Signed 64-bit integer)
  • UNSIGNED: Convert value to UNSIGNED (Unsigned 64-bit integer)
  • BINARY: Convert value to BINARY (binary string)
Required. Character set to convert to
数据类型 描述
CHAR(size) 一个固定长度字符串(可以包含字母,数字和特殊字符)。 size参数以字符为单位指定列长度 - 可以是0到255.默认值为1
VARCHAR(size) 保存可变长度的字符串(可以包含字母,数字和特殊字符)。 size参数指定字符的最大列长度 - 可以是0到65535
BINARY(size) 等于CHAR(),但存储二进制字节字符串。 size参数指定列长度(以字节为单位)。 默认值为1
VARBINARY(size) 等于VARCHAR(),但存储二进制字节字符串。 size参数指定最大列长度(以字节为单位)。
TINYBLOB 对于BLOB(二进制大对象)。 最大长度:255个字节
TINYTEXT 包含最大长度为255个字符的字符串
TEXT(size) 保存最大长度为65,535字节的字符串
BLOB(size) 对于BLOB(二进制大对象)。 最多可容纳65,535个字节的数据
MEDIUMTEXT 保存最大长度为16,777,215个字符的字符串
MEDIUMBLOB 对于BLOB(二进制大对象)。 最多可容纳16,777,215字节的数据
LONGTEXT 保存最大长度为4,294,967,295个字符的字符串
LONGBLOB 对于BLOB(二进制大对象)。 最多可容纳4,294,967,295字节的数据
ENUM(val1, val2, val3, ...) 一个字符串对象,只能有一个值,从可能值列表中选择。 您可以在ENUM列表中列出最多65535个值。 如果插入的值不在列表中,则将插入空值。 值按您输入的顺序排序
SET(val1, val2, val3, ...) 一个字符串对象,可以包含0个或更多值,从可能值列表中选择。 您可以在SET列表中列出最多64个值数字数据类型:

【相关推荐:mysql视频教程

The above is the detailed content of How to convert specified value to string type in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template