Solved

Problem in case statement in sql

Posted on 2014-10-01
4
186 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 49

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 49

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
running code or pseudo code of table structure 5 36
MySQL Memory Keeps Increasing 4 65
mysql database, schema and table creation 13 91
Concat multiple records into one line 3 45
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…

726 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