Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    120

    Unanswered: Combining two field results

    Hi all

    My application saves customer email addresses in two different fields in ym table

    How do i combine two fields from the same table in a select statement?

    I've tried the following:
    Select EmailAddy + ', ' + FriendsEmail AS Expr1
    FROM dbo.Contacts

    But all i get are the results from one field



    Andy

  2. #2
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    This code is a bit more defensive in case one of the two columns is NULL:
    Code:
    Select COALESCE(EmailAddy, '') + ', ' + COALESCE(FriendsEmail, '') AS EMails
    FROM dbo.Contacts
    But all i get are the results from one field
    Your code looks OK, as far as I can tell. I'd say, then only one field contains data.
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

  3. #3
    Join Date
    Feb 2008
    Posts
    120
    Thanks Wim :-)

    Works a treat

    Andy

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    As a minor tweak to Wim's code...
    Code:
    Select Coalesce(EmailAddy + ', ', '') + Coalesce(FriendsEmail, '') As Emails
    FROM   dbo.Contacts
    George
    Home | Blog

  5. #5
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    As a minor tweak to George's code...
    Code:
    Select Coalesce(EmailAddy, '') + 
    	CASE WHEN EmailAddy + FriendsEmail IS NOT NULL THEN ', ' ELSE '' END + 
    	Coalesce(FriendsEmail, '') As Emails
    FROM dbo.Contacts
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •