MySQL日期格式将dd.mm.yy转换为YYYY-MM-DD?
使用MySQL的STR_TO_DATE()方法进行转换。语法如下,其中我们使用格式说明符。格式说明符以%开头。
SELECT STR_TO_DATE(yourDateColumnName,'%d.%m.%Y') as anyVariableName FROM yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下。
create table ConvertIntoDateFormat -> ( -> Id int NOT NULL AUTO_INCREMENT, -> LoginDate varchar(30), -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
insert into ConvertIntoDateFormat(LoginDate) values('11.01.2019');
insert into ConvertIntoDateFormat(LoginDate) values('10.04.2017');
insert into ConvertIntoDateFormat(LoginDate) values('21.10.2016');
insert into ConvertIntoDateFormat(LoginDate) values('26.09.2018');
insert into ConvertIntoDateFormat(LoginDate) values('25.12.2012');使用select语句显示表中的所有记录。查询如下-
select *from ConvertIntoDateFormat;
以下是输出。
+----+------------+ | Id | LoginDate | +----+------------+ | 1 | 11.01.2019 | | 2 | 10.04.2017 | | 3 | 21.10.2016 | | 4 | 26.09.2018 | | 5 | 25.12.2012 | +----+------------+ 5 rows in set (0.00 sec)
以下是将日期格式设置为YYYY-MM-DD的查询。
select str_to_date(LoginDate,'%d.%m.%Y') as DateFormat from ConvertIntoDateFormat;
这是输出。
+------------+ | DateFormat | +------------+ | 2019-01-11 | | 2017-04-10 | | 2016-10-21 | | 2018-09-26 | | 2012-12-25 | +------------+ 5 rows in set (0.00 sec)
您也可以出于相同的目的使用DATE_FORMAT()方法。查询如下
select DATE_FORMAT(STR_TO_DATE(LoginDate,'%d.%m.%Y'), '%Y-%m-%d') as DateFormat from -> ConvertIntoDateFormat;
以下是输出-
+------------+ | DateFormat | +------------+ | 2019-01-11 | | 2017-04-10 | | 2016-10-21 | | 2018-09-26 | | 2012-12-25 | +------------+ 5 rows in set (0.00 sec)
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练