bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL References
SQL•SQL References

MySQL CAST() Function

Concept visual

MySQL CAST() Function

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Example

Convert a value to a DATE datatype:

Select

CAST("2017-08-29" AS DATE);

Definition and Usage

The CAST() function converts a value (of any type) into the specified datatype.

Tip:

See also the CONVERT() function.

Syntax

Cast(

value AS datatype )

Parameter Values

Parameter

Description value

Required. The value to convert datatype Required. The datatype to convert to. Can be one of the following:

Value

Description

Date

Formula

Converts value to DATE. Format: "YYYY - MM - DD"

Datetime

Formula

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

Decimal

Converts value to DECIMAL. Use the optional M and D parameters to specify the maximum number of digits (M) and the number of digits following the decimal point (D).

Time

Converts value to TIME. Format: "HH:MM:SS"

Char

Converts value to CHAR (a fixed length string)

Nchar

Converts value to NCHAR (like CHAR, but produces a string with the national character set)

Signed

Formula

Converts value to SIGNED (a signed 64 - bit integer)

Unsigned

Formula

Converts value to UNSIGNED (an unsigned 64 - bit integer)

Binary

Converts value to BINARY (a binary string)

Technical Details

Works in:

From MySQL 4.0

More Examples

Example

Convert a value to a CHAR datatype:

SELECT CAST(150 AS CHAR);

Previous

MySQL CASE Function

Next

MySQL COALESCE() Function