gpt4 book ai didi

mysql - 如何在 MySQL 中将列从 varchar 转换为 decimal,知道值是用逗号分隔的

转载 作者:行者123 更新时间:2023-11-29 07:35:58 25 4
gpt4 key购买 nike

我尝试了以下 MySQL 请求将列从 varchar 类型转换为 decimal 类型,但出现错误。

ALTER TABLE `cmd-services` CHANGE `Qte Sortie` `Qte Sortie` CAST(REPLACE(`Qte Sortie`, ',', '.') as DECIMAL(20,2));

ALTER TABLE `cmd-services` MODIFY `Qte Sortie` CAST(REPLACE(`Qte Sortie`, ',', '.') as DECIMAL(20,2));

我需要你的帮助。谢谢。

最佳答案

分两步完成

  1. 更新列并将所有非数字值设置为空,以便更改不会失败。
  2. 修改表并将类型设置为int

第一步:

UPDATE `cmd-services`
set `Qte Sortie` = CAST(REPLACE(`Qte Sortie`, ',', '.') as DECIMAL(20,2));

第 2 步:

ALTER TABLE `cmd-services`
MODIFY `Qte Sortie` DECIMAL(20,2);

This takes the assumption that the columns allow nulls. If not then that needs to be handled as well.

For example: By altering the column to allow null, then after it has been converted to DECIMAL then set all null values to 0 and alter the table to not allow null.

关于mysql - 如何在 MySQL 中将列从 varchar 转换为 decimal,知道值是用逗号分隔的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48868903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com