我正在尝试将具有所有以VARCHAR存储的值的数据库转换为正确的列类型,例如:INT,DATE,DECIMAL等。
问题是,列date_order
,date_billed
和date_paid
以不同的格式存储
我仍然遇到1292 Truncated incorrect date value: '05/18/2011'
错误,几乎不知道该怎么做,因为该确切的日期格式被列为CASE
我的代码:
SELECT
CAST(`ar_no` AS UNSIGNED),
CAST(`accession_id` AS UNSIGNED),
CAST(`client_id` AS UNSIGNED),
CAST(`insurance_id` AS UNSIGNED),
CAST(`test_id` AS UNSIGNED),
CASE
WHEN `date_paid` = '0' THEN `date_paid` = '00/00/0000'
WHEN LENGTH(DATE(STR_TO_DATE(`date_order`, '%m/%d/%y'))) IS NOT NULL THEN STR_TO_DATE(`date_order`, '%m/%d/%y')
WHEN LENGTH(DATE(STR_TO_DATE(`date_order`, '%m/%d/%Y'))) IS NOT NULL THEN STR_TO_DATE(`date_order`, '%m/%d/%Y')
END,
CASE
WHEN `date_paid` = '0' THEN `date_paid` = '00/00/0000'
WHEN LENGTH(DATE(STR_TO_DATE(`date_billed`, '%m/%d/%y'))) IS NOT NULL THEN STR_TO_DATE(`date_billed`, '%m/%d/%y')
WHEN LENGTH(DATE(STR_TO_DATE(`date_billed`, '%m/%d/%Y'))) IS NOT NULL THEN STR_TO_DATE(`date_billed`, '%m/%d/%Y')
END,
CASE
WHEN `date_paid` = '0' THEN `date_paid` = '00/00/0000'
WHEN LENGTH(DATE(STR_TO_DATE(`date_paid`, '%m/%d/%y'))) IS NOT NULL THEN STR_TO_DATE(`date_paid`, '%m/%d/%y')
WHEN LENGTH(DATE(STR_TO_DATE(`date_paid`, '%m/%d/%Y'))) IS NOT NULL THEN STR_TO_DATE(`date_paid`, '%m/%d/%Y')
END,
CAST(`amount_billed` AS DECIMAL(15,2)),
CAST(`amount_received` AS DECIMAL(15,2)),
CAST(`amount_adjusted` AS DECIMAL(15,2))
FROM `acs`.`billing_unformatted`;
最佳答案
Mysql仅排除YYYY-MM-DD
格式的日期。这就是为什么出现诸如1292 Truncated incorrect date value: '05/18/2011'
之类的错误的原因
您可以在导入之前在Excel中进行更改。
为了更改Excel中的日期格式:右键单击顶部的单元格。从下拉列表中选择format cells
。将local
更改为'Afrikans'
。选择看起来像2001-03-14
的格式。使用顶部的单元格fill down
。然后保存文档。
快速说明:Excel有时会做太多事情,并将此列还原为English(U.S)
默认时区。因此,如果您打算进行更多编辑,请确保该列尚未还原。
这是dev.mysql上更多字符串文字的链接。
此存储过程和帖子可能也对您有帮助:Error code 1292