SQL(phpMyAdmin): 使用命令更改日期的列格式
SQL(phpMyAdmin): Change Column Format of Date With Command
我有一个名称为 'expdate' 的列,类型为 'varchar',其值如下:
2021-02-27
28-02-2023
29/02/2024
.
.
.
现在我想将格式 %d-%m-%Y 等所有日期值更改为 %Y-%m-%d 并保存到日期类型为新的列并使用:
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(REPLACE(`expdate`,"/",'-'), "%d-%m-%Y")
where `expdate` != '0000-00-00'
但是 sql 命令显示如下错误:
SQL query:
UPDATE `Users` SET `fixed_expdate` = STR_TO_DATE(REPLACE(`expdate`,"/",'-'), "%d-%m-%Y") where `expdate` != '0000-00-00'
MySQL said: Documentation
#1411 - Incorrect datetime value: '2021-02-27' for function str_to_date
我需要sql命令,你的解决方案是什么?
您必须为每个单独的源数据格式使用不同的转换表达式。并且您必须检查是否只有与当前查询中使用的模式匹配的值才包含在转换中。
这可以使用许多单独的查询来执行,每个查询转换一种现有的源格式。
-- convert the dates which have correct format
UPDATE `Users`
SET `fixed_expdate` = `expdate`
WHERE `expdate` REGEXP '\d\d\d\d-\d\d-\d\d';
-- convert dated which looks like 28-02-2023
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(`expdate`, "%d-%m-%Y")
WHERE `expdate` REGEXP '\d\d-\d\d-\d\d\d\d';
-- convert dated which looks like 29/02/2024
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(`expdate`, "%d\/%m\/%Y")
WHERE `expdate` REGEXP '\d\d\/\d\d\/\d\d\d\d';
-- and so on
每次单独转换后(或多次转换后)您可能会查看未转换的行
SELECT `expdate`
FROM `Users`
WHERE `fixed_expdate` IS NULL
LIMIT XXX
并为尚未使用的模式构建下一个转换表达式 - 直到所有值都成功转换。
MySQL said: Documentation
#1411 - Incorrect datetime value: '2021-02-27' for function str_to_date
这是由于 2 种不同的格式,它们在日期和月份的占有(互换)方面不同 - 它们不容易区分。如果数据中同时存在这两种格式,则必须进行额外检查。如果两种格式都可能(例如,当值为 '01/02/2021'
)时,您无法区分必须应用哪种格式。
更新(从评论中复制)
Your regular expressions should should probably have anchors for the beginning and end of the string ('^' and '$'). – Gordon Linoff
我有一个名称为 'expdate' 的列,类型为 'varchar',其值如下:
2021-02-27
28-02-2023
29/02/2024
.
.
.
现在我想将格式 %d-%m-%Y 等所有日期值更改为 %Y-%m-%d 并保存到日期类型为新的列并使用:
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(REPLACE(`expdate`,"/",'-'), "%d-%m-%Y")
where `expdate` != '0000-00-00'
但是 sql 命令显示如下错误:
SQL query: UPDATE `Users` SET `fixed_expdate` = STR_TO_DATE(REPLACE(`expdate`,"/",'-'), "%d-%m-%Y") where `expdate` != '0000-00-00' MySQL said: Documentation #1411 - Incorrect datetime value: '2021-02-27' for function str_to_date
我需要sql命令,你的解决方案是什么?
您必须为每个单独的源数据格式使用不同的转换表达式。并且您必须检查是否只有与当前查询中使用的模式匹配的值才包含在转换中。
这可以使用许多单独的查询来执行,每个查询转换一种现有的源格式。
-- convert the dates which have correct format
UPDATE `Users`
SET `fixed_expdate` = `expdate`
WHERE `expdate` REGEXP '\d\d\d\d-\d\d-\d\d';
-- convert dated which looks like 28-02-2023
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(`expdate`, "%d-%m-%Y")
WHERE `expdate` REGEXP '\d\d-\d\d-\d\d\d\d';
-- convert dated which looks like 29/02/2024
UPDATE `Users`
SET `fixed_expdate` = STR_TO_DATE(`expdate`, "%d\/%m\/%Y")
WHERE `expdate` REGEXP '\d\d\/\d\d\/\d\d\d\d';
-- and so on
每次单独转换后(或多次转换后)您可能会查看未转换的行
SELECT `expdate`
FROM `Users`
WHERE `fixed_expdate` IS NULL
LIMIT XXX
并为尚未使用的模式构建下一个转换表达式 - 直到所有值都成功转换。
MySQL said: Documentation #1411 - Incorrect datetime value: '2021-02-27' for function str_to_date
这是由于 2 种不同的格式,它们在日期和月份的占有(互换)方面不同 - 它们不容易区分。如果数据中同时存在这两种格式,则必须进行额外检查。如果两种格式都可能(例如,当值为 '01/02/2021'
)时,您无法区分必须应用哪种格式。
更新(从评论中复制)
Your regular expressions should should probably have anchors for the beginning and end of the string ('^' and '$'). – Gordon Linoff