Solved

Problem in case statement in sql

Posted on 2014-10-01
4
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 50

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 50

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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