
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to use the CAST function in a MySQL SELECT statement?
The CAST() function in MySQL converts a value of any type into a value that has a specified type. Let us first create a table −
mysql> create table castFunctionDemo -> ( -> ShippingDate date -> ); Query OK, 0 rows affected (0.74 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into castFunctionDemo values('2019-01-31'); Query OK, 1 row affected (0.20 sec) mysql> insert into castFunctionDemo values('2018-07-12'); Query OK, 1 row affected (0.16 sec) mysql> insert into castFunctionDemo values('2016-12-06'); Query OK, 1 row affected (0.16 sec) mysql> insert into castFunctionDemo values('2017-08-25'); Query OK, 1 row affected (0.19 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from castFunctionDemo;
This will produce the following output −
+--------------+ | ShippingDate | +--------------+ | 2019-01-31 | | 2018-07-12 | | 2016-12-06 | | 2017-08-25 | +--------------+ 4 rows in set (0.00 sec)
Here is the query to use the cast() function correctly in a MySQL select statement −
mysql> select CAST(ShippingDate AS CHAR(12)) as Conversion FROM castFunctionDemo;
This will produce the following output −
+------------+ | Conversion | +------------+ | 2019-01-31 | | 2018-07-12 | | 2016-12-06 | | 2017-08-25 | +------------+ 4 rows in set (0.00 sec)
- Related Articles
- How can I use MySQL IF() function within SELECT statement?
- How to use NULL in MySQL SELECT statement?
- How to use a select statement while updating in MySQL?
- IF() function in a MySQL Select statement?
- How can I use a SELECT statement as an argument of MySQL IF() function?
- How to use prepared statement for select query in Java with MySQL?
- Can we use SELECT NULL statement in a MySQL query?
- How can we use MySQL SELECT statement to count number of rows in a table?
- Explain the use of SELECT DISTINCT statement in MySQL using Python?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- MySQL case statement inside a select statement?
- How to create Tab Delimited Select statement in MySQL?
- How to call a stored procedure using select statement in MySQL?
- MySQL procedure to display a “select” statement twice
- How to use alias in MySQL select query?

Advertisements