Cast Function

This section describes the CAST() function, which can be used to convert a value of any datatype to any other datatype.

"CAST(value AS type)" - Returns the casted value of the specified value as the specified type.

Examples of cast function, CastFunctions.sql:

-- CastFunctions.sql
-- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
--
SELECT PI() = '3.141593';
SELECT CAST(PI() AS CHAR) = '3.141593';
SELECT SUBSTRING(CAST(TIME'10:20:30' AS CHAR),4,2);
SELECT CAST(SUBSTRING('JAN 10, 1999',5,2) AS SIGNED) + 1;

Output:

PI() = '3.141593'
0
CAST(PI() AS CHAR) = '3.141593'
1
SUBSTRING(CAST(TIME'10:20:30' AS CHAR),4,2)
20
CAST(SUBSTRING('JAN 10, 1999',5,2) AS SIGNED) + 1
11

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

Commonly Used Functions

 Flow Control Functions

 Character String Functions

 Numeric Functions

 Date and Time Functions

Cast Function

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version