Solved

QUERY CASE WHEN PROBLEM

Posted on 2013-02-02
2
364 Views
Last Modified: 2013-02-02
Hi All,

I have query below, and I get compile error.

 SELECT
 A.*
, CASE CAST(A.MasaKerja AS MONEY)
      WHEN < 2 THEN 10000
      WHEN < 3 THEN 20000
      WHEN < 4 THEN 30000
      WHEN < 5 THEN 40000
      WHEN > 5 THEN 50000
ELSE 0
 END AS NilaiTunjangan
FROM
(
SELECT
EmpCode
, CASE WHEN RESIGNDATE IS NULL
THEN DATEDIFF(DAY, CONVERT(CHAR(8), EmployDate, 112), '20130131') / 365.0
ELSE DATEDIFF(DAY, CONVERT(CHAR(8), EmployDate, 112), CONVERT(CHAR(8), ResignDate, 112)) / 365.0
END AS MasaKerja
FROM THI_PAYROLL.dbo.TMEMPLOYEE
WHERE  (RESIGNDATE IS NULL OR
CONVERT(CHAR(8), ResignDate, 112) > '20130101')
) A
WHERE MasaKerja >= 1

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '<'.
Msg 102, Level 15, State 1, Line 22
Incorrect syntax near 'A'.

What's wrong with the code ?

Thank you.
0
Comment
Question by:emi_sastra
2 Comments
 
LVL 16

Accepted Solution

by:
kmslogic earned 500 total points
ID: 38847761
If you aren't testing for a specific value you'll have to put the condition on each when line like

CASE 
      WHEN CAST(A.MasaKerja AS MONEY) < 2 THEN 10000
      WHEN CAST(A.MasaKerja AS MONEY) < 3 THEN 20000
      WHEN CAST(A.MasaKerja AS MONEY)< 4 THEN 30000
      WHEN CAST(A.MasaKerja AS MONEY)< 5 THEN 40000
      WHEN CAST(A.MasaKerja AS MONEY)> 5 THEN 50000
ELSE 0
 END AS NilaiTunjangan 

Open in new window

0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 38847782
Got it.

Thank you very much for your help.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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