Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In database record as follows (Table Name Designation)

Name      Designation    Headtype
Ram       Courseofficer     0
Sekar     Director          1

i am putting case query as follows
when Headtype 0 means  i want text    courseofficer
when Headtype 1 means  i want text    Authorisedsignatoury.


for that query as follows

SQL
Select designation , (Case when Head_Type = '0' then 'Courseofficer'
 When Head_Type = '1' then 'Authorisedsignatory' else Head_Type end) as Type
from Designations


But below error message is displayed:
Conversion failed when converting the varchar value '<code>courseofficer</code>' to data type bit.


What am i doing wrong?
Posted
Updated 11-Aug-15 22:38pm
v3
Comments
Andy Lanng 12-Aug-15 4:32am    
please post what datatypes are in the table. Specifically, what datatype is "Head_Type"?

Try this:
SQL
SELECT [Name], CASE WHEN [Designation]='Director' THEN 'Authorisedsignatory' ELSE [Designation] END AS NewDesignation
FROM Designation

or
SQL
SELECT [Name], CASE WHEN [Headtype]=1 THEN 'Authorisedsignatory' ELSE [Designation] END AS NewDesignation
FROM Designation
 
Share this answer
 
Comments
Wendelius 14-Aug-15 0:02am    
My 5.
Maciej Los 15-Aug-15 10:54am    
Thank you, Mika.
The problem occurs because you have two different data types returned from the case structure. The Head_Type is a bit and you're returning it in the else branch. In all other branches you're returning character data. Because there is no implicit conversion from bit to varchar you face the error

So to correct this you should explicitely convert the bit. Something like the following:
SQL
Select designation , 
       (Case 
           when Head_Type = '0' then 'Courseofficer'
           When Head_Type = '1' then 'Authorisedsignatory' 
           else cast(Head_Type as varchar) 
        end) as Type
from Designations

The next thing is that the field you compare is a bit. With bit you shouldn't use char comparison since it causes an extra conversion. So the case would be better with
SQL
Select designation , 
       (Case 
           when Head_Type = 0 then 'Courseofficer'
           When Head_Type = 1 then 'Authorisedsignatory' 
           else cast(Head_Type as varchar) 
        end) as Type
from Designations

since you're using just a single value comparison I would also simplify the case structure to
SQL
Select designation ,
       (Case Head_Type
           when 0 then 'Courseofficer'
           When 1 then 'Authorisedsignatory'
           else cast(Head_Type as varchar)
        end) as Type
from Designations

Also as Maciej Los[^] pointed out in the first solution, the condition for the case could be simplified to use the already existing data.


As a side note The else branch handles the situation that the head_type is possibly null so you may need to adjust the code to better handle such situation...
 
Share this answer
 
v2
Comments
Maciej Los 15-Aug-15 11:00am    
Well explained. A5!
Wendelius 15-Aug-15 12:05pm    
Thank you Maciej!
SQL
Select designation , Case when Head_Type = '0' then 'Courseofficer'
 When Head_Type = '1' then 'Authorisedsignatory' else Head_Type end as Type
from Designations

end as "type" mean.. the name of the "type" you seperated by end) as type
end as "des" mean column name will be des

SQL
Select designation , Case when Head_Type = '0' then 'Courseofficer'
 When Head_Type = '1' then 'Authorisedsignatory' else Head_Type end
from Designations


The second one make the output with any specified column name,It just show (No column name)
if u use bracket () mean stop with only end dont use..as requires column name
SQL
Select designation , (Case when Head_Type = '0' then 'Courseofficer'
 When Head_Type = '1' then 'Authorisedsignatory' else Head_Type end)
from Designations
 
Share this answer
 
v4

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