Error: Error Converting data type nvarchar to Numeric

Asked

Viewed 3,477 times

0

I am trying to convert data from a column in NVARCHAR(255) to DECIMAL(10,2), however, all the ways I tried always gives the same error Error Converting data type nvarchar to Numeric. I created another column of type DECIMAL(10,2) to transfer the data from the column of type NVARCHAR(255), but even with CONVERT or CAST also does not convert. Follow the code below:


    SELECT CASE 
             WHEN Isnumeric(salario)  0 THEN CONVERT(NVARCHAR(255), 
                                             CONVERT(DECIMAL(10, 2), salario)) 
             ELSE salario 
           END AS [SALÁRIO] 
    FROM dbo.rendimentos 

  • 1

    How is the format of this string in the database? maybe it is in an invalid format to convert to decimal. Something else. may be giving error because the column is null or empty string.

  • The salary column is set to allow Nulls values, but has no null value. Using the ISNUMERIC() function, all the data in this column return the value 1, that is, all are numerical. Even so, it returns the conversion error.

  • @Kellysoares What is the decimal separator: semicolon?

1 answer

0


Evaluate the following suggestion:

-- código #1  v2
SELECT salario,
       convert(decimal(10, 2), salario) as [SALÁRIO] 
  from dbo.rendimentos
  where Isnumeric(salario) = 1;

However, if the column values salario use the comma as decimal separator, the error occurs. If this is the case, the correct code is:

 -- código #2 v3
 SELECT salario,
        convert(decimal(10, 2), replace(salario, ',', '.')) as [SALÁRIO] 
  from dbo.rendimentos
  where Isnumeric(salario) = 1;

Demo: http://sqlfiddle.com/#! 18/3f02a/1

  • Good afternoon Jose! Your first code was perfect. It had a table with more than 20,000 records and only one comma to separate the decimal places. Because of him all the code was giving problem and had not noticed. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.