MySQL查询从日期时间转换为日期?
您可以使用CAST()MySQL中的函数来实现此目的。语法如下-
SELECT CAST(yourColumnName as Date) as anyVariableName from yourTableName;
为了理解上述语法,让我们首先创建一个表。创建表的查询如下-
mysql> create table ConvertDateTimeToDate -> ( -> ArrivalDatetime datetime -> );
使用插入命令将日期时间插入表中。查询如下-
mysql> insert into ConvertDateTimeToDate values(date_add(now(),interval -1 year));
mysql> insert into ConvertDateTimeToDate values('2017-11-21 13:10:20');
mysql> insert into ConvertDateTimeToDate values('2016-05-24 21:11:24');
mysql> insert into ConvertDateTimeToDate values('2012-04-30 04:05:50');现在,让我们使用select命令显示表中的所有记录。查询如下。
mysql> select *from ConvertDateTimeToDate;
以下是输出。
+---------------------+ | ArrivalDatetime | +---------------------+ | 2017-12-27 10:05:21 | | 2017-11-21 13:10:20 | | 2016-05-24 21:11:24 | | 2012-04-30 04:05:50 | +---------------------+ 4 rows in set (0.00 sec)
这是在MySQL中从datetime转换为date的查询。
mysql> select cast(ArrivalDatetime as Date) as Date from ConvertDateTimeToDate;
以下是输出。
+------------+ | Date | +------------+ | 2017-12-27 | | 2017-11-21 | | 2016-05-24 | | 2012-04-30 | +------------+ 4 rows in set (0.00 sec)
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短