CAST()函数,把一个字段转成另一个字段,主要转化的是字段的类型
其语法为:cast(字段名 as 转换的类型 )
转换的类型共有: CHAR 字符型
DATE 日期型
DATETIME 日期和时间型
DECIMAL float型
SIGNED int型
TIME 时间型
比如在表table1中有一列叫 date,它的值为 2015-11-03 15:31:26
通过cast()转换后的结果为:
select cast(date as signed) as date from table1;
-- 结果:
20151103153126
select cast(date as char) as date from table1;
-- 结果:
2015-11-03 15:31:26
select cast(date as datetime) as date from table1;
-- 结果
2015-11-03 15:31:26
select cast(date as date) as date from table1;
-- 结果
2015-11-03
select cast(date as time) as date from table1;
-- 结果
15:31:26