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
select * from AssignmentMarks a where try_cast(a.Marks as decimal(5,2)) is null and a.Marks not in ('+','-+','-') and a.Marks is not null and a.Marks != ''