Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

QUERY CASE WHEN PROBLEM

Posted on 2013-02-02
2
Medium Priority
?
369 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
[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 Comments
 
LVL 16

Accepted Solution

by:
kmslogic earned 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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, …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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