将列值从 VARCHAR(n) 转换为 DECIMAL 时出现问题

发布于 2024-08-28 06:45:25 字数 718 浏览 5 评论 0原文

我有一个 SQL Server 2000 数据库,其中有一列类型为 VARCHAR(255)。所有数据要么为 NULL,要么为最多两点精度的数字数据(例如“11.85”)。我尝试运行以下 T-SQL 查询,但收到错误“将数据类型 varchar 转换为数字时出错”。

SELECT CAST([MyColumn] AS DECIMAL)
FROM [MyTable];

我尝试了更具体的转换,但也失败了。

SELECT CAST([MyColumn] AS DECIMAL(6,2))
FROM [MyTable];

我还尝试了以下操作来查看是否有任何数据是非数字的,并且返回的唯一值是 NULL。

SELECT ISNUMERIC([MyColumn]), [MyColumn]
FROM [MyTable]
WHERE ISNUMERIC([MyColumn]) = 0;

我尝试转换为其他数据类型,例如FLOAT和MONEY,但只有MONEY成功。所以我尝试了以下方法:

SELECT CAST(CAST([MyColumn] AS MONEY) AS DECIMAL)
FROM [MyTable];

...效果很好。原始查询失败的原因有什么想法吗?如果我先转换为 MONEY 再转换为 DECIMAL 会有问题吗?

谢谢!

I have a SQL Server 2000 database with a column of type VARCHAR(255). All the data is either NULL, or numeric data with up to two points of precision (e.g. '11.85'). I tried to run the following T-SQL query but received the error 'Error converting data type varchar to numeric'

SELECT CAST([MyColumn] AS DECIMAL)
FROM [MyTable];

I tried a more specific cast, which also failed.

SELECT CAST([MyColumn] AS DECIMAL(6,2))
FROM [MyTable];

I also tried the following to see if any data is non-numeric, and the only values returned were NULL.

SELECT ISNUMERIC([MyColumn]), [MyColumn]
FROM [MyTable]
WHERE ISNUMERIC([MyColumn]) = 0;

I tried to convert to other data types, such as FLOAT and MONEY, but only MONEY was successful. So I tried the following:

SELECT CAST(CAST([MyColumn] AS MONEY) AS DECIMAL)
FROM [MyTable];

...which worked just fine. Any ideas why the original query failed? Will there be a problem if I first convert to MONEY and then to DECIMAL?

Thanks!

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

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

发布评论

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

评论(3

孤独陪着我 2024-09-04 06:45:25

这可能取决于小数符号是逗号还是点。以下是一些测试查询及其结果:

select CAST('3.6' as decimal) -- 3.60
select CAST('3,6' as decimal) -- Error converting data type varchar to numeric.
select CAST('3.6' as money) -- 3.60
select CAST('3,6' as money) -- 36.00 (!)
select ISNUMERIC('3.6') -- 1
select ISNUMERIC('3,6') -- 1

ISNUMERIC< 的文档/a> 说:

ISNUMERIC 当输入时返回 1
表达式的计算结果为有效
整数、浮点数、金钱
或十进制类型;否则它返回
0

3.63,6 都可以转换为 money,所以这就是为什么 isnumeric('3,6') 返回<代码>1。

要解决此问题,请将逗号替换为点(或者反之亦然,如果您的系统使用逗号作为小数点符号):

select cast(replace('3,6',',','.') as decimal)

另一种选择是更改 Windows 中的“小数点符号”。它位于配置面板 ->区域和语言 ->格式->附加设置->数字。

It's likely that this depends on whether the decimal symbol is a comma or a dot. Here are some test queries and their results:

select CAST('3.6' as decimal) -- 3.60
select CAST('3,6' as decimal) -- Error converting data type varchar to numeric.
select CAST('3.6' as money) -- 3.60
select CAST('3,6' as money) -- 36.00 (!)
select ISNUMERIC('3.6') -- 1
select ISNUMERIC('3,6') -- 1

The documentation for ISNUMERIC says :

ISNUMERIC returns 1 when the input
expression evaluates to a valid
integer, floating point number, money
or decimal type; otherwise it returns
0

Both 3.6 and 3,6 can be cast to money, so that's why isnumeric('3,6') returns 1.

To resolve this issue, replace the comma's with dots (or vice versa, if your system uses comma as the decimal symbol):

select cast(replace('3,6',',','.') as decimal)

Another option is to change the "decimal symbol" in Windows. It's in Config Panel -> Regional and Language -> Formats -> Additional Settings -> Numbers.

我不会写诗 2024-09-04 06:45:25

另一个原因是空字符串。例如,如果您使用导入向导导入 CSV 文件,空字段将变为空字符串,而不是空值。

这是解决此问题的常见习惯用法:

CAST(NULLIF([number_as_a_string],'') AS decimal(13,2))

Another cause is empty strings. For example, if you use the import wizard to import a CSV file, empty fields will become empty strings, not nulls.

This is a common idiom for fixing this:

CAST(NULLIF([number_as_a_string],'') AS decimal(13,2))
2024-09-04 06:45:25

选择 CAST(isnull(MyColumn,0) 作为 Decimal(4,2))
来自[我的表];

Select CAST(isnull(MyColumn,0) as Decimal(4,2))
FROM [MyTable];

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