Solved

Error when using Case statement in SQL View

Posted on 2011-02-10
5
243 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
5 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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
Comment Utility
I think you mean:
Incorrect syntax near '‘'.
0
 
LVL 13

Expert Comment

by:agarwalrahul
Comment Utility
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
Comment Utility
agarwalrahul,

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

Author Comment

by:imstac73
Comment Utility
agarwalrahul,, didn't work.  Incorrect syntax near '='.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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