Error: Error converting data type nvarchar to numeric

0

I'm trying to convert data from a column in NVARCHAR (255) to DECIMAL (10,2), however, all the ways I tried it 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 of the column of type NVARCHAR (255), but even with CONVERT or CAST also does not convert. Here is 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 

    
asked by anonymous 12.03.2018 / 21:54

1 answer

0

Rate 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 values in the salario column use the comma as the decimal place 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: link

    
13.03.2018 / 23:48