Solved

Problem in case statement in sql

Posted on 2014-10-01
4
184 Views
Last Modified: 2014-10-01
Question : Select first_name, incentive amount from employee and incentives table for all employees even if they didn't get incentives and set incentive amount as 0 for those employees who didn't get incentives.

My Query is :

select first_name, case Incentive_amount when null then 0 end from Employee_Table left join incentive on Employee_Table.EMPLOYEE_ID = incentive.EMPLOYEE_ID
0
Comment
Question by:satmisha
  • 2
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40354750
Almost. Or you use an ELSE keyword or the ISNULL function.
SELECT first_name, CASE Incentive_amount 
           WHEN IS NULL THEN 0 
          ELSE Incentive_amount 
          END
FROM Employee_Table 
LEFT JOIN incentive ON Employee_Table.EMPLOYEE_ID = incentive.EMPLOYEE_ID 

Open in new window

SELECT first_name, ISNULL(Incentive_amount,0) 
FROM Employee_Table 
LEFT JOIN incentive ON Employee_Table.EMPLOYEE_ID = incentive.EMPLOYEE_ID 

Open in new window

0
 

Author Comment

by:satmisha
ID: 40354794
In the above Query Showing error

Incorrect syntax near the keyword 'IS'.
0
 

Author Comment

by:satmisha
ID: 40354811
In the First  Query  using case Showing error

Incorrect syntax near the keyword 'IS'.
0
 
LVL 47

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40354816
Sorry. Should be like this:
SELECT first_name, CASE  
           WHEN Incentive_amount IS NULL THEN 0 
          ELSE Incentive_amount 
          END
FROM Employee_Table 
LEFT JOIN incentive ON Employee_Table.EMPLOYEE_ID = incentive.EMPLOYEE_ID 

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question