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

x
?
Solved

Case when CHARINDEX

Posted on 2006-11-21
5
Medium Priority
?
934 Views
Last Modified: 2008-02-26
Got two emails:
john.smith@yahoo.com
jsmith@yahoo.com

Need to execute the following logic:
upper(substring(HQ_Ops.infoteam.Project_Locations."Project Manager",1,charindex('.',HQ_Ops.infoteam.Project_Locations."Project Manager")-1)) + ' ' + upper(substring(HQ_Ops.infoteam.Project_Locations."Project Manager", charindex('.',HQ_Ops.infoteam.Project_Locations."Project Manager")+1,charindex('@',HQ_Ops.infoteam.Project_Locations."Project Manager")-charindex('.',HQ_Ops.infoteam.Project_Locations."Project Manager")-1))

The problem is when the email address, 'jsmith@yahoo.com',  doesn't contain '.' then the statement  fails.
In this case I need to  bring just the part before "@", in this example JSMITH.

Thus, I need to write CASE WHEN statement that would capture missing '.' and bring back just 'jsmith' otherwise executes the statement above 'JOHN SMITH'
Any help would be welcome!
0
Comment
Question by:InfoTeam
[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
5 Comments
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17991172
Hi InfoTeam,

case
    when charindex('.',HQ_Ops.infoteam.Project_Locations."Project Manager") = 0 then
        substring(HQ_Ops.infoteam.Project_Locations."Project Manager",1,charindex('@',HQ_Ops.infoteam.Project_Locations."Project Manager") -1)
    else
        -- your statement here
end


Cheers,
Hillwaaa
0
 

Author Comment

by:InfoTeam
ID: 17995895
The problem with the statement is that "." exists in yahoo.com and that's why it wouldn't equal 0. We should just try the first part jsmith@
0
 

Author Comment

by:InfoTeam
ID: 17996064
Seems like this one works:

CASE WHEN charindex('.', substring(HQ_Ops.infoteam.Project_Locations.[Project Manager], 1, charindex('@',
                      HQ_Ops.infoteam.Project_Locations.[Project Manager]) - 1)) = 0 THEN upper(substring(HQ_Ops.infoteam.Project_Locations.[Project Manager], 1,
                      charindex('@', HQ_Ops.infoteam.Project_Locations.[Project Manager]) - 1))
                      ELSE upper(substring(HQ_Ops.infoteam.Project_Locations.[Project Manager], 1, charindex('.', HQ_Ops.infoteam.Project_Locations.[Project Manager])
                      - 1)) + ' ' + upper(substring(HQ_Ops.infoteam.Project_Locations.[Project Manager], charindex('.', HQ_Ops.infoteam.Project_Locations.[Project Manager])
                      + 1, charindex('@', HQ_Ops.infoteam.Project_Locations.[Project Manager]) - charindex('.', HQ_Ops.infoteam.Project_Locations.[Project Manager]) - 1))
                      END
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18298378
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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