Nvarchar to Decimal and then sum it up - sql

I have a nvarchar column and I need to convert it to Decimal. I tried the following code and it worked.
(CONVERT(decimal(2,2),CellContent,2))
Now when I tried to add a sum for the above code it is throwing me error
SUM (CONVERT(decimal(2,2),CellContent,2)) - Function Not recognised
How do I sum up the cell content field??

An arithmetic overflow error means SQL can't fit the nvarchar value in the number of positions you specified for the Decimal data type.
What values are you using for the CellContent column?
Try Decimal(12,2) maybe?
SUM (CONVERT(decimal(12,2),CellContent,2))

Related

SQL convert nvarchar into decimal dynamic rows

I am not so familiar with using convert in SQL so thats why i am stuck with the following situation:
The column has a nvarchar(max) and i would like to convert it into decimal (18,2). But there are some rows that consist a "full" amount (see red box). For all values with the full amount i would like to have it as 1222,00
When creating a SQL view I got this error:
Error converting data type nvarchar to numeric.
How can i still convert this column into decimal?
Many thanks
From your pictures it seems to me like you are using SQL-Server. If so your problem is having , as decimal point instead of . It has nothing to do with having whole and decimal numbers in your data.
So you should replace it before converting data.
SELECT CONVERT(decimal(18,2), REPLACE(ColumnName,',','.')) FROM TableName
DB<>fiddle

How to format a SSRS field with 2-digits of procession when the data is a mix of formats

I have data from a Stored Procedure that looks like this and needs to display on my report with 2-digits of precision. The data is all NVARCHAR but there is a mix of rows that looks numeric or says 'Max'.
How do I display only 2 digits of precision? I tried the following which works great on the numeric looking data but errors on 'Max'.
=FormatNumber(Fields!TBS.Value, 2)
Use an IIF to determine if the value is a number and IIF so, format it.
=IIF(ISNUMERIC(Fields!TBS.Value), FormatNumber(Fields!TBS.Value, 2), Fields!TBS.Value)
IsNumeric:
Returns a Boolean value indicating whether an expression can be
evaluated as a number.
MS SSRS Description

Trying to sum two columns in SQL Server results in error message

I'm trying to calculate the sum of two separate fields with the query
select sum(percent12 + percent21) as total
from finalquery
but I keep getting this error:
Msg 8117, Level 16, State 1, Line 535
Operand data type varchar is invalid for sum operator.
However, if I do:
select percent12 + percent21 as total
from finalquery
I get:
(total)
50.0040.00
25.0025.00
100.0 0.00
100.0 0.00
100.0 0.00
How can I fix this?
Both of your percentage columns are a varchar, the + operator can concatenate strings together, and that is what you're seeing.
I would suggest that you use a CAST to a NUMERIC data type, especially as it seems that you have a fixed amount of spaces after the decimal.
Try the following:
SELECT
SUM(CAST(percent12 AS NUMERIC(10,2))+ CAST(percent21 AS NUMERIC(10,2))) AS total
FROM finalquery
Others have suggested that you CAST to a FLOAT, though that has known rounding errors (and known workarounds)
Float rounding error: SQL Server rounding Error, Giving different values
It seems that the values you are trying to sum are varchar/string type and therefore they cannot be summed up.
Try convert them to integer or float (depending of the type) before summing
ie at SQL Server Select sum(convert(float,x1) + convert(float,x2) )
When you are using the + operator in your second query, you are actually concatenating two strings. You know they are strings because the error message tells you it's a varchar (i.e. a string).
Instead, you need to convert each field into a number data type and then wrap those in your aggregation. In this case, I chose to cast it as a decimal data type based off of your sample data, but you can cast as integer or other numeric data type as well.
SELECT SUM(CAST(percent12 AS DECIMAL(10, 4)) + CAST(percent21 AS DECIMAL(10, 4))) AS total
FROM finalquery
Of course, the obvious answer is to stop storing numbers as strings. If you can modify your database schema, seriously look into changing that.
Fix your schema and you will solve this problem and prevent many others in the future. You should choose the appropriate datatype for each column - don't just blindly select some type of string because it's "easier".

Round a value to two decimal places in SQL

I am trying to round a value in SQL, here is the code that I have:
select round(600.000,2)
How do I get the value 600.00?
Instead of round() convert to a decimal:
select cast(600.000 + 0.5 as decimal(10, 2) )
round() changes the value but it might not change the type of the result. Hence, you might still see extra decimal points (depending on the database and the application). Converting to a decimal with two digits of precision converts both the value and the type.

Arithmetic overflow error converting float error in sql

I am getting this error
Arithmetic overflow error converting float to data type numeric
when I try to run my view but not sure what am I doing wrong with my calculation. I have researched but could not solve it so far.
Here is the line of code that is causing the error:
ISNULL(CAST(CAST(TOTAL_APPTS.APPT_CNT AS FLOAT) / TOTAL_RECS.PAT_CNT AS NUMERIC(3, 2)), 0) AS [CONVERSION RATE]
Your precision and scale arguments to NUMERIC are very small. Have you tried increasing those? Your numeric value can only handle numbers up to 9.99.
You should peruse this page:
decimal and numeric (Transact-SQL)
It's too much to explain here, but basically the first argument (precision) is the max number of digits (in your case 3) and the second argument (scale) is the number of digits to the right of the decimal point, which always takes away from the number of digits you can have to the left of the decimal point. So in your case, 3-2 = 1 digit allowed to the left of the decimal point, which is why your max value can only be 9.99.