Skip to content
Advertisement

How do I update “case when” kogi mosi [closed]

update [dbo].[student]
set marks = case
when marks > 60 then 'Good'
when marks >= 40 and marks <= 60 then 'Okay'
when marks < 40 then 'Fail' end
--where marks in (60, 40)

select * from student

I already have a table with some sample data. When I execute this I get: Error converting data type varchar to numeric.

Advertisement

Answer

Aside from the issue you have of trying to update the same column you are comparing with using a string data type, what you should probably consider is either putting this logic in a view, or you could add a computed / generated column.

The syntax might vary [slightly] depending on your RDBMS.

Alter table dbo.Student
add [column] Results as
case
 when marks > 60 then 'Good'
 when marks >= 40 and marks <= 60 then 'Okay'
 else 'Fail' 
end
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement