MySQL - STR_TO_DATE() 函数
MySQL STR_TO_DATE() 函数将字符串格式的日期转换为所需格式的日期。
此函数接受字符串形式的日期值和格式字符串作为参数,从给定格式的字符串中提取 DATE、TIME 或 DATETIME 值并返回结果。
您可以使用某些具有预定义含义的字符来创建格式字符串参数。它们是:-
格式 | 描述 |
---|---|
%a | Weekday name (Sun..Sat) |
%b | Month name (Jan..Dec) |
%c | Month, numeric (0..12) |
%D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, ...) |
%d | Day of the month, numeric (00..31) |
%e | Day of the month, numeric (0..31) |
%f | Microseconds (000000..999999) |
%H | Hour (00..23) |
%h | Hour (01..12) |
%I | Hour (01..12) |
%i | Minutes, numeric (00..59) |
%j | Day of year (001..366) |
%k | Hour (0..23) |
%l | Hour (1..12) |
%M | Month name (January..December) |
%m | Month, numeric (00..12) |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM) |
%S | Seconds (00..59) |
%s | Seconds (00..59) |
%T | Time, 24-hour (hh:mm:ss) |
%U | Week (00..53), where Sunday is the first day of the week; WEEK() mode 0 |
%u | Week (00..53), where Monday is the first day of the week; WEEK() mode 1 |
%V | Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X |
%v | Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x |
%W | Weekday name (Sunday..Saturday) |
%w | Day of the week (0=Sunday..6=Saturday) |
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal % character |
语法
以下是 MySQL STR_TO_DATE() 函数的语法 -
STR_TO_DATE(str,format)
参数
此方法接受两个参数。如下所示:-
str: 包含日期或时间信息的字符串。
format: 字符串中日期或时间信息的表示格式。
返回值
此函数根据提供的字符串和格式返回 MySQL 日期或时间值。
示例
在以下查询中,我们使用 STR_TO_DATE() 函数将给定的字符串转换为日期格式 -
SELECT STR_TO_DATE('2023 年 9 月 5 日星期六', '%D %W %M %Y') As Result;
输出
这将产生以下结果 -
Result |
---|
2023-09-05 |
示例
以下是此函数的另一个示例 -
SELECT STR_TO_DATE('Sat Sep 05 23', '%a %b %d %y') As Result;
输出
以下是输出结果 -
Result |
---|
2023-09-05 |
示例
这里,我们使用 STR_TO_DATE() 函数将字符串"20 小时 40 分钟 45 秒"转换为时间格式 -
SELECT STR_TO_DATE('20 小时 40 分钟 45 秒', '%H 小时 %i 分钟 %S 秒') As Result;
输出
以下是输出结果 -
Result |
---|
20:40:45 |
示例
以下查询将日期时间字符串转换为 DATETIME 值 -
SELECT STR_TO_DATE('Sep 05 15 10:23:00 PM', '%b %d %y %r') As Result;
输出
以下是输出 -
Result |
---|
2015-09-05 22:23:00 |
示例
在此示例中,我们使用以下 CREATE TABLE 查询创建了一个名为 PLAYERS 的表 -
CREATE TABLE PLAYERS( ID int, NAME varchar(255), DOB varchar(255), Country varchar(255), PRIMARY KEY (ID) );
现在,我们将使用 INSERT 语句在 MyPlayers 表中插入 7 条记录 -
INSERT INTO PLAYERS VALUES (1, 'Shikhar Dhawan', '5th December 1981, Saturday', 'India'), (2, 'Jonathan Trott', '22nd April 1981, Wednesday', 'SouthAfrica'), (3, 'Kumara Sangakkara', '27th October 1977, Thursday', 'Srilanka'), (4, 'Virat Kohli', '5th November 1988, Saturday', 'India'), (5, 'Rohit Sharma', '30th April 1987, Thursday', 'India'), (6, 'Ravindra Jadeja', '6th December 1988, Tuesday', 'India'), (7, 'James Anderson', '30th June 1982, Wednesday', 'England');
执行以下查询以获取上述创建的表中的所有插入记录 -
Select * From PLAYERS;
以下是 PLAYERS 表 -
ID | NAME | DOB | Country |
---|---|---|---|
1 | Shikhar Dhawan | 5th December 1981, Saturday | India |
2 | Jonathan Trott | 22nd April 1981, Wednesday | Srilanka |
3 | Kumara Sangakkara | 27th October 1977, Thursday | Srilanka |
4 | Virat Kohli | 5th November 1988, Saturday | India |
5 | Rohit Sharma | 30th April 1987, Thursday | India |
6 | Ravindra Jadeja | 30th April 1987, Thursday | India |
7 | James Anderson | 30th June 1982, Wednesday | England |
在这里,我们使用 MySQL STR_TO_DATE() 函数将"DOB"值从其当前格式转换为新格式指定的格式字符串。
SELECT ID, NAME, DOB, STR_TO_DATE(DOB, '%D %M %Y, %W') As Result FROM PLAYERS;
输出
输出显示如下 −
ID | NAME | DOB | Result |
---|---|---|---|
1 | Shikhar Dhawan | 5th December 1981, Saturday | 1981-12-05 |
2 | Jonathan Trott | 22nd April 1981, Wednesday | 1981-04-22 |
3 | Kumara Sangakkara | 27th October 1977, Thursday | 1977-10-27 |
4 | Virat Kohli | 5th November 1988, Saturday | 1988-11-05 |
5 | Rohit Sharma | 30th April 1987, Thursday | 1987-04-30 |
6 | Ravindra Jadeja | 30th April 1987, Thursday | 1988-12-06 |
7 | James Anderson | 30th June 1982, Wednesday | 1982-06-30 |