Easy computed column question

i'm moving a database from Access to SQL Server and trying to create computed columns that do the work of Access Visual Basic routines.  Here are the routines I'm trying to convert:

'example: "Mr.", "John", "Doe" "jdoe@email.com" -> "John Doe <jdoe@email.com>"
'example: "Dr.", "John", "Doe" "jdoe@email.com" -> "Dr. John Doe <jdoe@email.com>"
'example: "", "John", "Doe" "jdoe@email.com" -> "John Doe <jdoe@email.com>"
Public Function getFullEmailFor(Prefix, FirstName, LastName, Email)
    getFullEmailFor = getFullNameAndTitleFor(Prefix, FirstName, LastName) & " <" & Email & ">"
End Function

Public Function getFullNameAndTitleFor(Prefix, FirstName, LastName)
    If (Prefix <> "Mr.") And (Prefix <> "Mrs.") And (Prefix <> "Ms.") Then
        newPrefix = Prefix
    Else       'Dr. Rev. Capt. etc
        newPrefix = ""
    End If
    getFullNameAndTitleFor = newPrefix & " " & FirstName & " " & LastName
End Function
spathiphylumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
alter table yourtable add FullNameAndTitleFor = FirstName + ' ' + LastName + ' <' + Email + '>'
 
0
spathiphylumAuthor Commented:
wow.  thats fast.  that's a good start, but it doesn't deal with the prefix:  "Dr.", "Mr.", etc.   see the examples in the question
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, I didn't see you had 2 functions ..

alter table yourtable
  add FullNameAndTitleFor = coalesce( Prefix + ' ' , '' ) + FirstName + ' ' + LastName



alter table yourtable
  add FullEmailFor = FirstName + ' ' + LastName + ' <' + Email + '>'
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

spathiphylumAuthor Commented:
here are the two examples:

'example: "Mr.", "John", "Doe" "jdoe@email.com" -> "John Doe <jdoe@email.com>"
'example: "Dr.", "John", "Doe" "jdoe@email.com" -> "Dr. John Doe <jdoe@email.com>"

note that if the second one has the title, but not the first.  i don't want to show the title if it is "Mr."  "Mrs."  "Ms."
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry... it's time for me to go to bed :-)



alter table yourtable
  add FullEmailFor = coalesce( Prefix + ' ' , '' ) +FirstName + ' ' + LastName + ' <' + Email + '>'
0
spathiphylumAuthor Commented:
that's not going to work.   if the title is "Mr." then it will print "Mr. John Doe"  instead of "John Doe" (see the examples above)
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok, Mr, Mrs, Ms are not to be used...

plugging in a case statement should do that:

alter table yourtable
  add FullEmailFor = coalesce( case when Prefix in ('Mr.', 'Mrs.', 'Ms.') then null else prefix end + ' ' , '' ) +FirstName + ' ' + LastName + ' <' + Email + '>'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.