Usage of CAST() function in MYSQL

The CAST() function converts one field into another field. Compared with orcale, MySQL is much simpler, and only needs a Cast() function to do it. Its syntax is: Cast (type of field name as conversion), where the type can be:

CHAR[(N)] Character 
type DATE Date type
DATETIME Date and time type
DECIMAL float type
SIGNED int
TIME Time type

For example table table1

date

2015-11-03 15:31:26

select cast(date as signed) as date from  table1;

The result is as follows:

date

20151103153126

select cast(date as char) as date from  table1;

The result is as follows:

date

2015-11-03 15:31:26

select cast(date as datetime) as date from  table1;

The result is as follows:

date

2015-11-03 15:31:26

select cast(date as date) as date from  table1;

The result is as follows:

date

2015-11-03

select cast(date as time) as date from  table1;

The result is as follows:

date

15:31:26

Here date corresponds to date, time corresponds to time

 

The CAST() function converts one field into another field. Compared with orcale, MySQL is much simpler, and only needs a Cast() function to do it. Its syntax is: Cast (type of field name as conversion), where the type can be:

CHAR[(N)] Character 
type DATE Date type
DATETIME Date and time type
DECIMAL float type
SIGNED int
TIME Time type

For example table table1

date

2015-11-03 15:31:26

select cast(date as signed) as date from  table1;

The result is as follows:

date

20151103153126

select cast(date as char) as date from  table1;

The result is as follows:

date

2015-11-03 15:31:26

select cast(date as datetime) as date from  table1;

The result is as follows:

date

2015-11-03 15:31:26

select cast(date as date) as date from  table1;

The result is as follows:

date

2015-11-03

select cast(date as time) as date from  table1;

The result is as follows:

date

15:31:26

Here date corresponds to date, time corresponds to time

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324601071&siteId=291194637