Solved

Problem in case statement in sql

Posted on 2014-10-01
4
176 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 45

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 45

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

22 Experts available now in Live!

Get 1:1 Help Now