Solved

QUERY CASE WHEN PROBLEM

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

10 Experts available now in Live!

Get 1:1 Help Now