首页 文章资讯内容详情

在MySQL中将VARCHAR日期转换为其他格式?

2026-06-04 1 花语

假设您已将日期设置为VARCHAR格式。现在,如果要更新格式,请使用UPDATE命令和STR_TO_DATE()。语法如下-

update yourTableName set yourColumnName=str_to_date(yourColumnName,%m/%d/%Y);

让我们首先创建一个表-

create table DemoTable ( DueDate varchar(100) );

使用插入命令在表中插入一些记录-

insert into DemoTable values(12/01/2019); insert into DemoTable values(01/31/2016); insert into DemoTable values(03/17/2018); insert into DemoTable values(07/25/2017);

使用select语句显示表中的所有记录-

select *from DemoTable;

这将产生以下输出-

+------------+ | DueDate | +------------+ | 12/01/2019 | | 01/31/2016 | | 03/17/2018 | | 07/25/2017 | +------------+ 4 rows in set (0.00 sec)

以下是在MySQL中将varchar数据转换为其他格式的查询-

update DemoTable set DueDate=str_to_date(DueDate,%m/%d/%Y); Rows matched: 4 Changed: 4 Warnings: 0

让我们再次检查表记录-

select *from DemoTable;

这将产生以下输出-

+------------+ | DueDate | +------------+ | 2019-12-01 | | 2016-01-31 | | 2018-03-17 | | 2017-07-25 | +------------+ 4 rows in set (0.00 sec)