Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2003
    Posts
    22

    Unanswered: Merging text fields in view

    I'm using a view where I do a few things, including merge two short text fields into one. Just wanted to know if there was an alternative way of doing this:

    Example:
    SELECT first_name + ' ' + last_name AS full_name
    FROM names

    It works - but it seems rather blah. Is there something that'd be more "proper"?

  2. #2
    Join Date
    Mar 2004
    Posts
    12
    Not sure why you consider it "blah". You're concatenating fields and renaming the result, not very unusual. You may want to add some rtrim or isnull to protect yourself from bad data.

    SELECT isnull(rtrim(first_name) + ' ' + rtrim(last_name),'') AS full_name
    FROM names
    Howard Nugent
    Sr. Business Application Analyst

  3. #3
    Join Date
    Jul 2003
    Posts
    22
    I just felt like it was a bit "too easy", kind of like when you write a billion lines of code and suddenly your program works without any quirks, but on a smaller scale.

    Good suggestion on the null and trim, thanks.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    i would use coalesce instead of isnull (because it's standard sql)

    but more importantly, i would apply it to the individual fields

    because some people have one name null, but you still want the other name, not an empty string after the concatenation has nulled it out
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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