要将DateTime值转换为MySQL中的字符串,可以使用DATE_FORMAT()函数。语法如下-
select date_format(yourColumnName, ‘%d %m %y’) as anyVariableName from yourTableName;
为了理解上述概念,让我们创建一个表。创建表的查询如下-
mysql> create table DateAsStringDemo -> ( -> YourDateTime datetime -> );
在curdate()
方法的帮助下插入日期。查询插入日期如下-
mysql> insert into DateAsStringDemo values(curdate());
让我们借助select语句显示所有记录。查询如下-
mysql> select *from DateAsStringDemo;
以下是输出-
+---------------------+ | YourDateTime | +---------------------+ | 2018-11-26 00:00:00 | +---------------------+ 1 row in set (0.00 sec)
将日期转换为字符串的查询如下-
mysql> select date_format(YourDateTime,'%d %m %y') as YourDateAsString from DateAsStringDemo;
以下是输出-
+------------------+ | YourDateAsString | +------------------+ | 26 11 18 | +------------------+ 1 row in set (0.00 sec)