Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. CONCAT names with condition

CONCAT names with condition

Scheduled Pinned Locked Moved Database
comannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I have first_name, middle_name and last_name in my customers table. I want to do

    SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)

    The middle name is optional so I want to ignore it if it's blank to be:

    SELECT CONCAT(first_name, ' ', last_name)

    because if I do it the normal way I'll have extra space between the first_name and last_name

    Technology News @ www.JassimRahma.com

    M P G 3 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I have first_name, middle_name and last_name in my customers table. I want to do

      SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)

      The middle name is optional so I want to ignore it if it's blank to be:

      SELECT CONCAT(first_name, ' ', last_name)

      because if I do it the normal way I'll have extra space between the first_name and last_name

      Technology News @ www.JassimRahma.com

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You can use ISNULL on the first middle name or a case statement. Case statement is more robust as it caters for ''.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, I have first_name, middle_name and last_name in my customers table. I want to do

        SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)

        The middle name is optional so I want to ignore it if it's blank to be:

        SELECT CONCAT(first_name, ' ', last_name)

        because if I do it the normal way I'll have extra space between the first_name and last_name

        Technology News @ www.JassimRahma.com

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        As long as you're doing it that way, you might as well use REPLACE. :-D

        You'll never get very far if all you do is follow instructions.

        1 Reply Last reply
        0
        • J Jassim Rahma

          Hi, I have first_name, middle_name and last_name in my customers table. I want to do

          SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)

          The middle name is optional so I want to ignore it if it's blank to be:

          SELECT CONCAT(first_name, ' ', last_name)

          because if I do it the normal way I'll have extra space between the first_name and last_name

          Technology News @ www.JassimRahma.com

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          Without using CONCAT you can do this:

          rtrim(isnull(first_name,'',first_name+' ') + isnull(middle_name,'',middle_name+' ') + isnull(last_name,'',last_name))

          The rtrim will remove a trailing space where the person does not have a last name.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups