简体   繁体   English

将数据类型 nvarchar(50) 转换为数值时出错

[英]Error converting data type nvarchar(50) to numeric

I have a price column with the type of NVARCHAR(50) .我有一个类型为NVARCHAR(50)的价格列。 Inside of this, I have data like 123,22 as a string and goes on.在这里面,我有像123,22这样的数据作为字符串并继续。 They are now string and I want to convert them to decimal(18,2) and I am trying to query this but giving me an error:它们现在是字符串,我想将它们转换为decimal(18,2) ,我试图查询这个但给了我一个错误:

UPDATE reservations
SET price = CONVERT(NVARCHAR(50),CONVERT(decimal(18, 2), price,105))
ALTER TABLE reservations
ALTER COLUMN price decimal(18, 2)

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

I have run this code before for the datetime and it worked but for decimal I am getting this error.我之前已经为datetime时间运行过这段代码,它可以工作,但对于decimal ,我收到了这个错误。 Can anyone tell me the problem?谁能告诉我这个问题?

I think you should replace comma to dot .我认为您应该将逗号替换为dot For example;例如;

DECLARE @price AS NVARCHAR(50)=N'123,22'

SELECT TRY_CONVERT(decimal(18, 2), REPLACE(@price,',','.'),105)

If we change your query according to this approach;如果我们根据这种方法更改您的查询;

UPDATE reservations
SET price= CONVERT(NVARCHAR(50),CONVERT(decimal(18, 2), REPLACE(price,',','.'),105))
ALTER TABLE reservations
ALTER COLUMN price decimal(18, 2)

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

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