使用CAST或STR_TO_DATE链接JSON_EXTRACT失败

时间:2022-04-27 15:26:11

I'm trying to extract a datetime from a JSONFIELD "data" in MySQL.

我正在尝试从MySQL中的JSONFIELD“数据”中提取日期时间。

If I do a simple JSON_EXTRACT however, the return field type is a JSON.

但是,如果我执行简单的JSON_EXTRACT,则返回字段类型是JSON。

mysql> select JSON_EXTRACT(data, "$.new_time") from analytics limit 10;
+----------------------------------+
| JSON_EXTRACT(data, "$.new_time") |
+----------------------------------+
| NULL                             |
| "2016-09-30T04:00:00+00:00"      |
| "2016-09-29T05:30:00+00:00"      |
| NULL                             |
| "2016-10-01T05:30:00+00:00"      |
| "2016-09-27T23:00:00+00:00"      |
| NULL                             |
| "2016-09-23T01:30:00+00:00"      |
| "2016-09-23T04:00:00+00:00"      |
| "2016-09-27T01:30:00+00:00"      |
+----------------------------------+

I want to convert this to a MySQL DATETIME. But if I chain JSON_EXTRACT and STR_TO_DATETIME, I get all NULL values:

我想将其转换为MySQL DATETIME。但如果我链接JSON_EXTRACT和STR_TO_DATETIME,我会得到所有NULL值:

mysql> select STR_TO_DATE(JSON_EXTRACT(data, "$.new_time") ,"%Y-%m-%d") from analytics_calendaranalytics limit 10;
+-----------------------------------------------------------+
| STR_TO_DATE(JSON_EXTRACT(data, "$.new_time") ,"%Y-%m-%d") |
+-----------------------------------------------------------+
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
| NULL                                                      |
+-----------------------------------------------------------+

Likewise, a CAST as DATETIME also fails:

同样,CAST作为DATETIME也会失败:

mysql> select CAST(JSON_EXTRACT(data, "$.new_time") as DATETIME) from analytics_calendaranalytics limit 10;
+----------------------------------------------------+
| CAST(JSON_EXTRACT(data, "$.new_time") as DATETIME) |
+----------------------------------------------------+
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
| NULL                                               |
+----------------------------------------------------+

Both these commands work when I start with the string value:

当我从字符串值开始时,这两个命令都有效:

mysql> select CAST("2016-09-30T04:00:00+00:00" as DATETIME);
+-----------------------------------------------+
| CAST("2016-09-30T04:00:00+00:00" as DATETIME) |
+-----------------------------------------------+
| 2016-09-30 04:00:00                           |
+-----------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select STR_TO_DATE("2016-09-30T04:00:00+00:00", "%Y-%m-%d");
+------------------------------------------------------+
| STR_TO_DATE("2016-09-30T04:00:00+00:00", "%Y-%m-%d") |
+------------------------------------------------------+
| 2016-09-30                                           |
+------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

Would appreciate any help in resolving this!

非常感谢您解决这个问题的任何帮助!

1 个解决方案

#1


6  

You have to use JSON_UNQUOTE

你必须使用JSON_UNQUOTE

select CAST( JSON_UNQUOTE( JSON_EXTRACT(data, "$.new_time")) as DATETIME) from analytics_calendaranalytics limit 10;

Would work. I am saying would because you haven't provided sample data. I tried as follows:

会工作。我说是因为你没有提供样本数据。我尝试如下:

select @js := JSON_OBJECT('new_time',"2016-09-30T04:00:00+00:00"  );

select CAST(JSON_UNQUOTE(JSON_EXTRACT(@js,'$.new_time')) as DATETIME);

The following query also works

以下查询也有效

 select STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(@js,'$.new_time')) ,"%Y-%m-%d");

#1


6  

You have to use JSON_UNQUOTE

你必须使用JSON_UNQUOTE

select CAST( JSON_UNQUOTE( JSON_EXTRACT(data, "$.new_time")) as DATETIME) from analytics_calendaranalytics limit 10;

Would work. I am saying would because you haven't provided sample data. I tried as follows:

会工作。我说是因为你没有提供样本数据。我尝试如下:

select @js := JSON_OBJECT('new_time',"2016-09-30T04:00:00+00:00"  );

select CAST(JSON_UNQUOTE(JSON_EXTRACT(@js,'$.new_time')) as DATETIME);

The following query also works

以下查询也有效

 select STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(@js,'$.new_time')) ,"%Y-%m-%d");