从varchar转换为datetime并在MySQL中进行比较?
为此,您可以使用STR_TO_DATE()。让我们首先创建一个表-
mysql> create table DemoTable1565-> (
-> ArrivalDatetime varchar(40)
-> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1565 values('10/01/2019 21:29:35');mysql> insert into DemoTable1565 values('10/13/2019 4:56:00');
mysql> insert into DemoTable1565 values('10/13/2018 12:40:46');
mysql> insert into DemoTable1565 values('10/13/2019 21:30:00');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1565;
这将产生以下输出-
+---------------------+| ArrivalDatetime |
+---------------------+
| 10/01/2019 21:29:35 |
| 10/13/2019 4:56:00 |
| 10/13/2018 12:40:46 |
| 10/13/2019 21:30:00 |
+---------------------+
4 rows in set (0.00 sec)
当前日期如下-
mysql> select now();+---------------------+
| now() |
+---------------------+
| 2019-10-13 21:34:14 |
+---------------------+
1 row in set (0.00 sec)
这是将varchar转换为datetime并进行比较的查询-
mysql> select * from DemoTable1565 where str_to_date(ArrivalDatetime,'%m/%d/%Y')=curdate();
这将产生以下输出-
+---------------------+| ArrivalDatetime |
+---------------------+
| 10/13/2019 4:56:00 |
| 10/13/2019 21:30:00 |
+---------------------+
2 rows in set, 4 warnings (0.03 sec)
以上是 从varchar转换为datetime并在MySQL中进行比较? 的全部内容, 来源链接: utcz.com/z/340917.html