Solved

Problem in case statement in sql

Posted on 2014-10-01
4
181 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 46

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Update Query 23 93
Duplicating MySQL Table columns 5 31
Instering to MySQL table 5 37
problems with mysql ODBC Connector and vbscript 4 23
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Creating and Managing Databases with phpMyAdmin in cPanel.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now