Click here to Skip to main content
15,891,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
select empno, ename, sal, 
    case when deptno = 10 then ' Low Salary'
	when deptno = 20 then ' Middle Sal '
	when deptno = 30 then ' High Sal '
	else 0
	end
	from emp1


What I have tried:

when i am trying to write this error, it gives this error, i do not understand why it gives this error

Msg 245, Level 16, State 1, Line 14
Conversion failed when converting the varchar value ' Middle Sal ' to data type int.
Posted
Updated 5-Dec-21 3:49am
Comments
Richard MacCutchan 5-Dec-21 9:41am    
You cannot select a string of text into an integer.

1 solution

It's because deptno is a numeric field and you're trying to return a string OR a numeric 0 in place of it (see that ELSE clause.) That won't work.

You have to do something like this:
SQL
SELECT empno, ename, sal, "SalaryRange" = 
    CASE WHEN deptno = 10 THEN ' Low Sal '
        WHEN deptno = 20 THEN ' Middle Sal '
        WHEN deptno = 30 THEN ' High Sal '
        ELSE ' Unknown '
    END
FROM emp1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900