?
Solved

Error when using Case statement in SQL View

Posted on 2011-02-10
5
Medium Priority
?
261 Views
Last Modified: 2012-06-27
I'm trying to use a CASE statement in my SQL View.

I keep getting the following error; can't figure out what is wrong with my code.

Error message: Incorrect Syntax near ''.
SELECT     

CASE WHEN Right(Email, 5) = ‘a.com’

THEN

LEFT(Email, LEN(Email) - 21) + UPPER(SUBSTRING(Email, LEN(Email) - 20, 1)) + SUBSTRING(Email, LEN(Email) - 19, 6) + UPPER(SUBSTRING(Email, 
                      LEN(Email) - 13, 1)) + SUBSTRING(Email, LEN(Email) - 12, 6) + UPPER(SUBSTRING(Email, LEN(Email) - 6, 1)) + RIGHT(Email, 6) 

ELSE 

LEFT(Email, LEN(Email) - 11) + UPPER(SUBSTRING(Email, LEN(Email) - 10, 1))  + SUBSTRING(Email, LEN(Email) - 9, 2) 
 + UPPER(SUBSTRING(Email, LEN(Email) - 7, 1))  + Right(Email,7)

AS Email

FROM         dbo.AD_List

Open in new window

0
Comment
Question by:imstac73
[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
5 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 34866750
SELECT     

CASE WHEN Right(Email, 5) = 'a.com'

THEN

LEFT(Email, LEN(Email) - 21) + UPPER(SUBSTRING(Email, LEN(Email) - 20, 1)) + SUBSTRING(Email, LEN(Email) - 19, 6) + UPPER(SUBSTRING(Email, 
                      LEN(Email) - 13, 1)) + SUBSTRING(Email, LEN(Email) - 12, 6) + UPPER(SUBSTRING(Email, LEN(Email) - 6, 1)) + RIGHT(Email, 6) 

ELSE 

LEFT(Email, LEN(Email) - 11) + UPPER(SUBSTRING(Email, LEN(Email) - 10, 1))  + SUBSTRING(Email, LEN(Email) - 9, 2) 
 + UPPER(SUBSTRING(Email, LEN(Email) - 7, 1))  + Right(Email,7)

end AS Email

FROM         dbo.AD_List

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34867810
I think you mean:
Incorrect syntax near '‘'.
0
 
LVL 13

Expert Comment

by:Rahul Agarwal
ID: 34868145
Try this:


SELECT    

CASE Email WHEN Right(Email, 5) = ‘a.com’

THEN

LEFT(Email, LEN(Email) - 21) + UPPER(SUBSTRING(Email, LEN(Email) - 20, 1)) + SUBSTRING(Email, LEN(Email) - 19, 6) + UPPER(SUBSTRING(Email,
                      LEN(Email) - 13, 1)) + SUBSTRING(Email, LEN(Email) - 12, 6) + UPPER(SUBSTRING(Email, LEN(Email) - 6, 1)) + RIGHT(Email, 6)

ELSE

LEFT(Email, LEN(Email) - 11) + UPPER(SUBSTRING(Email, LEN(Email) - 10, 1))  + SUBSTRING(Email, LEN(Email) - 9, 2)
 + UPPER(SUBSTRING(Email, LEN(Email) - 7, 1))  + Right(Email,7)

AS Email

FROM         dbo.AD_List
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34868167
agarwalrahul,

Just out of curiousity, did you actually test that?
0
 

Author Comment

by:imstac73
ID: 34890706
agarwalrahul,, didn't work.  Incorrect syntax near '='.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

752 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