Solved

QUERY CASE WHEN PROBLEM

Posted on 2013-02-02
2
363 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach 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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 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