Solved

CASE Expressions in Sql server

Posted on 2009-05-20
13
205 Views
Last Modified: 2013-11-25
Hi i need to select a column and check if that column has value 'PS', if it has then i need to change the other column value to 'VNE' else WTE'. Here's my code , but iam getting the error message as invalid syntax near '=' . Please help me on this.

SELECT
 Eml.Fileno, eml.loc, eml.cmp=
 CASE
         WHEN eml.loc=='PS'
         THEN 'VNE' ELSE 'WTE' end
 
FROM
 Emp
  LEFT JOIN Eml ON Eml.Emp = Emp.Emp
  LEFT JOIN Audtrl ON Audtrl.srcid = emp.empid
 
WHERE

 Audtrl.AudtrlDt <= '20090515' and eml.fileno is not null)

also i want to do another query where eml.loc should not be selected,  but still need a CASE statement for the column eml.cmp  for the above mentioned functionality. Hope you understood my question.

thanks
0
Comment
Question by:gladstonesheeba
[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
  • 7
  • 6
13 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24435789
SELECT
 Eml.Fileno, eml.loc, eml.cmp=
 CASE
         WHEN eml.loc ='PS'
         THEN 'VNE' ELSE 'WTE' end
 
FROM
 Emp
  LEFT JOIN Eml ON Eml.Emp = Emp.Emp
  LEFT JOIN Audtrl ON Audtrl.srcid = emp.empid
 
WHERE

 Audtrl.AudtrlDt <= '20090515' and eml.fileno is not null)
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 500 total points
ID: 24435812
try this


SELECT
 Eml.Fileno, eml.loc,
 cmp= CASE WHEN eml.loc='PS'THEN 'VNE' ELSE 'WTE' end
FROM Emp
LEFT JOIN Eml ON Eml.Emp = Emp.Emp
LEFT JOIN Audtrl ON Audtrl.srcid = emp.empid
WHERE Audtrl.AudtrlDt <= '20090515' and eml.fileno is not null
0
 

Author Comment

by:gladstonesheeba
ID: 24435855
Still iam getting the same error message Invalid syntax near '=' at line 2
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24435869
did u try my second query
0
 

Author Comment

by:gladstonesheeba
ID: 24435891
Yes, i tried both the query, but still the same error.
0
 

Author Comment

by:gladstonesheeba
ID: 24435926
sorry,  i tried the second query and it worked. one thing i want to know, how to remove that loc column from the select query, but still i need the case expression for the cmp column.

For example

SELECT
 Eml.Fileno,
 cmp= CASE WHEN eml.loc='PS'THEN 'VNE' ELSE 'WTE' end
FROM Emp
LEFT JOIN Eml ON Eml.Emp = Emp.Emp
LEFT JOIN Audtrl ON Audtrl.srcid = emp.empid
WHERE Audtrl.AudtrlDt <= '20090515' and eml.fileno is not null

how to write a query for that.
0
 

Author Comment

by:gladstonesheeba
ID: 24436007
oh , iam sorry for the silly question. i figured it out my self. iam trying to change the column name for cmp , but iam getting the error mesage , invalid  syntax near the keyword 'as'

here it is

cmp= CASE WHEN eml.loc='PS'THEN 'VNE' ELSE 'WTE' end as 'company'

I would appreciate your help on this.

Thanks
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24436009
> how to remove that loc column from the select query
you mean within the 'Case' statement ? if you remove that, how will you compare the value and generate the 'CMP' column
0
 

Author Comment

by:gladstonesheeba
ID: 24436076
not within the CASE Statement , from the Select Statement,  just want to remove it from the select query not within the case statement.

instead of select eml.fileno, eml.loc, cmp=case  when eml.loc......

I want to just select these fields

eml.fileno, cmp=case when eml.loc .........

Hope you can find the difference between the two statements that i mentioned above.
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 500 total points
ID: 24436110
u jyst need to remove it from the SELECT list,

SELECT
 Eml.Fileno, emp= CASE WHEN eml.loc='PS'THEN 'VNE' ELSE 'WTE' end
FROM Emp
LEFT JOIN Eml ON Eml.Emp = Emp.Emp
LEFT JOIN Audtrl ON Audtrl.srcid = emp.empid
WHERE Audtrl.AudtrlDt <= '20090515' and eml.fileno is not null
0
 

Author Comment

by:gladstonesheeba
ID: 24436170
Hey Thanks for the reply. it worked.

how to change that cmp field name to "Company code" at the end. iam getting the error message if i do this

emp= CASE WHEN eml.loc='PS'THEN 'VNE' ELSE 'WTE' end as 'Company code'
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24436200
remove the 'Emp =' that will do

CASE WHEN eml.loc='PS'THEN 'VNE' ELSE 'WTE' end as 'Company code'
0
 

Author Closing Comment

by:gladstonesheeba
ID: 31583698
Thank you so much for your help . Excellent.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

749 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