• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

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
0
spathiphylum
Asked:
spathiphylum
  • 4
  • 3
1 Solution
 
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
Independent Software Vendors: 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!

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now