How do I store data when the Mysql5.7 virtual column data type is DATE?

Table structure: v_date is a virtual column

CREATE TABLE `test` (
    `json` TEXT NULL,
    `date` DATETIME NULL DEFAULT NULL,
    `v_date` DATE AS (json_extract(`json`,"$.date")) VIRTUAL
)
COMMENT="\r\n"
COLLATE="utf8mb4_general_ci"
ENGINE=InnoDB
;

insert:

INSERT INTO test (json) VALUES ("{"date":"2019-01-21 00:00:00"}");

error report:

ERROR 3156 (22018): Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column json_extract at row 1

tried various formats:

INSERT INTO `test`.`test_virtual_column` (`json`) VALUES ("{"date":"20191221"}");
/* SQL3156:Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column json_extract at row 1 */

INSERT INTO `test`.`test_virtual_column` (`json`) VALUES ("{"date":20191221}");
/* SQL3156:Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column 
json_extract at row 1 */

how do I save data of type Date to virtual columns?

May.13,2022

first use the json_unquote () function to remove the quotation marks from the JSON value, which is recommended when creating new virtual columns.

CREATE TABLE `test_virtual_column` (
    `json` JSON NULL DEFAULT NULL,
    `date` DATETIME NULL DEFAULT NULL,
    `v_date` DATETIME AS (json_unquote(json_extract(`json`,'$.date'))) VIRTUAL
)

feels that this problem is a little poles apart.
you should consider changing the time from the program point of view and unifying the output interface to the outside
otherwise, if you change the page display later, you will have to forcibly convert it to the JSON structure if you change the connection end

.
Menu