将带有百分比(%)百分比的大十进制转换为小数点

发布于 2025-01-25 11:02:20 字数 510 浏览 3 评论 0原文

我有以下数据需要转换为简单的小数。目前,它是varchar格式。数据在一个名为Commission%

佣金%(当前VARCHAR)佣金%V2(需要小数为小数)
的列中, 87.00000%.87
95.00000%.95

我尝试了以下内容:

CAST(CAST(CONVERT(FLOAT,REPLACE([Commission %], ''%'', '''')) / 100 AS DECIMAL(10,6)) as DECIMAL(10,6))

但是我收到以下错误消息错误转换错误。数据类型VARCHAR至数字。前几天我能够使用类似的东西,但是当时落后零的东西较少。

I have the following data that I need to convert to a simple decimal. Currently, it is in varchar format. The data is in a column called Commission%

Commission% (currently Varchar)Commission% V2 (needs to be decimal)
87.00000%.87
95.00000%.95

I have tried the following:

CAST(CAST(CONVERT(FLOAT,REPLACE([Commission %], ''%'', '''')) / 100 AS DECIMAL(10,6)) as DECIMAL(10,6))

BUT I get the following error message Error converting data type varchar to numeric. I was able to use something similar the other day but there were fewer trailing zero's at the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少跟Wǒ拽 2025-02-01 11:02:20

这几乎可以肯定是由于数据不良,这是您将数字存储为字符串时应该期望的。这是您识别它们的方式:

SELECT [KeyColumn], [Commission%] 
  FROM dbo.BadColumnNamesWow
  WHERE [Commission%] IS NOT NULL
    AND TRY_CONVERT(decimal(20,10), REPLACE([Commission%], '%', '')) IS NULL;

由于您现在说还有其他模式,因此仅仅这样做才能识别所有不良数据可能是有意义的:

SELECT * FROM dbo.BadColumnNamesWow
  WHERE [Commission%] IS NOT NULL
    AND PATINDEX('%[^0-9.]%', [Commission%]) > 0;

然后将其清理。这应该是为什么您切勿将数字值存储在字符串列中的课程。

This is almost certainly due to bad data which is what you should expect when you store numbers as strings. Here is how you identify them:

SELECT [KeyColumn], [Commission%] 
  FROM dbo.BadColumnNamesWow
  WHERE [Commission%] IS NOT NULL
    AND TRY_CONVERT(decimal(20,10), REPLACE([Commission%], '%', '')) IS NULL;

Since you now say there are other patterns, it might make sense to just do this to identify all of the bad data:

SELECT * FROM dbo.BadColumnNamesWow
  WHERE [Commission%] IS NOT NULL
    AND PATINDEX('%[^0-9.]%', [Commission%]) > 0;

Then clean it up. This should be a lesson in why you never store numeric values in string columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文