In the column [Return rate]
, I have values like :
20.0%
17.1%
etc
In my query, I want to use this values in a calculation.
So first, I replace the '%'
by an empty string ''
:
REPLACE([Return Rate], '%' ,'') AS [Test]
This works, and I get values like '20.0' when [Return rate] was '20.0%'.
Then I try to use this [Test] value in a calculation, for instance:
(REPLACE([Return Rate], '%' ,'') * 10) AS [Test]
But I logically obtain an error, so I try to convert this text value to perform my calculation:
CAST ( REPLACE([Current Xelus FE Return Rate], '%' ,'') AS decimal(2,1)) [Decimal Test]
And it's here that I get the error:
Arithmetic overflow error converting varchar to data type numeric. Warning: Null value is eliminated by an aggregate or other SET operation.
Has someone the answer to this error? Thanks a lot,
If any of your rows contain null values, you will get this error. Try an IsNull, like this:
CAST(
IsNull(
REPLACE([Current Xelus FE Return Rate], '%' ,'')
, '0.0')
AS decimal(5,1)) [Decimal Test]
If your data contains non-numeric values (as you mentioned, values like 'N/A'), you can elimimate them with the IsNumeric() function:
CAST(
CASE WHEN IsNumeric(
IsNull(
REPLACE([Current Xelus FE Return Rate], '%' ,'')
, '0.0')
) = 1 THEN IsNull(REPLACE([Current Xelus FE Return Rate], '%' ,''),'0.0')
ELSE '0.0'
END
AS decimal(5,1)) [Decimal Test]
Try casting it to FLOAT - decimal(2,1) is not enough:
CAST ( REPLACE([Current Xelus FE Return Rate], '%' ,'') AS FLOAT)
Then you should be able to do further calculations
CAST ( REPLACE([Current Xelus FE Return Rate], '%' ,'') AS FLOAT) * 10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.