I have a table called AssignmentMarks
that stores Student's assignment marks for different subjects.
This table has a column called Marks varchar(10)
which stores the marks. The marks can be like 1
, 2
, and also it can be +
, -
or -+
which represents each sign a specific marks.
Note: +
is 1
mark, -
is -1
and -+
is 0.5
While getting a student marks from the table I am facing the Arithmetic Overflow
error which I don't know what is the cause of this error.
The query is as follow:
SELECT SUM(
CAST(
CASE
WHEN a.Marks IS NULL OR a.Marks = '' THEN 0
WHEN a.Marks = '+' THEN 1
WHEN a.Marks = '-' THEN -1
WHEN a.Marks = '-+' THEN 0.5
ELSE a.Marks
END AS DECIMAL(5,2)
)
)
FROM AssignmentMarks AS a WHERE a.StudentID=10
AND a.SubjectID=1
After Executing the above query I get the following error.
Msg 8115, Level 16, State 8, Line 7
Arithmetic overflow error converting varchar to data type numeric.
Any idea what is the main cause of this error?
Sample Data: SQL Fiddle
Best Answer
From error message,
My first place where I could change is :
and change it to
or convert to decimal , if you want
Later edit:
In case expression you have a few THEN with different data types: int, numeric, varchar. Due to data type precedence, when combines different data types, the lower data type is converted to the type with higher precedence See here information about it.
you can see this , by this example: