简体   繁体   English

如何将nvarchar转换为十进制?

[英]how to cast nvarchar to decimal?

i know how to convert nvarchar to decimal(18,4) Cast method. 我知道如何将nvarchar转换为十进制(18,4)强制转换方法。 my tables rows count 80000. My query run perfect below... 我的表格行数为80000。我的查询在下面完美运行...


SELECT  top 80000 id, Cast(MH as decimal(18,4)) as MH 
FROM TaskRelations WHERE MH is not null

BUT; 但;

but below select query not WORK! 但下面选择查询不起作用! if write below: 如果写在下面:


SELECT   id, Cast(MH as decimal(18,4)) as MH 
FROM TaskRelations WHERE MH is not null

ERROR: Error converting data type nvarchar to numeric. 错误:将数据类型nvarchar转换为数字时出错。

You have non-numeric data in your MH field after row 80,000. 您的MH字段中的第80,000行之后有非数字数据。

You can use the ISNUMERIC function but it is notoriously buggy and will give you loads of false positives depending on your data. 您可以使用ISNUMERIC函数,但是它臭名昭著的错误,将根据您的数据为您带来大量误报。

The problem is usually that indeed some of the nvarchars in the MH columns are not convertible to decimal. 问题通常是,实际上MH列中的某些nvarchar不能转换为十进制。

You can try to inspect the data visually, but as you have more than 80k recordsm, that might not be feasible. 您可以尝试以视觉方式检查数据,但是由于记录数超过80k,这可能不可行。 Try to inspect the data in some other way - you can use isNumeric , or use group by on the first (or last) three characters of the column, to find the erring data. 尝试以其他方式检查数据-您可以使用isNumeric ,或在列的前(或后)三个字符处使用group by来查找错误的数据。

Basically the error your receiving is due to the fact that some record past the top 80000 records cannot be converted to decimal. 基本上,您收到的错误是由于以下事实造成的:超过最高80000条记录的某些记录无法转换为十进制。 Check your data on all rows > 80000 检查所有> 80000的行上的数据

try this: 尝试这个:

SELECT   max(MH) as max_mh, min(mh) as min_mh
FROM TaskRelations 
WHERE MH is not null

the non-numeric values should be lower or higher than the numeric ones. 非数字值应小于或大于数字值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM