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. General Programming
  3. Visual Basic
  4. How to merge three columns to one column?

How to merge three columns to one column?

Scheduled Pinned Locked Moved Visual Basic
tutorialdatabasehelpquestion
3 Posts 2 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.
  • V Offline
    V Offline
    viettho
    wrote on last edited by
    #1

    In my table, I have three columns: first_name, last_name, middle_name . I tried to merge them into one column, and this is what I did: select first_name + ' ' + last_name + ' ' + middle_name as Full_Name from contacts However, when I do a query on that one, only the ones that have data in all three columns show and if one of the column is missing data wouldn't show. for example, orginal table -------------------------------------- |first_name | last_name | Middle_name| -------------------------------------- test | test | null | -------------------------------------- when merges: ------------------------- | Full_Name | ------------------------- | null | ------------------------- if column middle_name is empty, then Full_name would return null, eventhough first_name and last_name have data, why is that happening? Could someone help me how to make that work? Thanks a bunch. John

    C 1 Reply Last reply
    0
    • V viettho

      In my table, I have three columns: first_name, last_name, middle_name . I tried to merge them into one column, and this is what I did: select first_name + ' ' + last_name + ' ' + middle_name as Full_Name from contacts However, when I do a query on that one, only the ones that have data in all three columns show and if one of the column is missing data wouldn't show. for example, orginal table -------------------------------------- |first_name | last_name | Middle_name| -------------------------------------- test | test | null | -------------------------------------- when merges: ------------------------- | Full_Name | ------------------------- | null | ------------------------- if column middle_name is empty, then Full_name would return null, eventhough first_name and last_name have data, why is that happening? Could someone help me how to make that work? Thanks a bunch. John

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      viettho wrote: why is that happening? Because interaction with null produces null. e.g. 1 + null = null viettho wrote: Could someone help me how to make that work? Yes.

      SELECT CASE first_name WHEN NULL THEN '' ELSE first_name + ' ' END +
      CASE last_name WHEN NULL THEN '' ELSE last_name + ' ' END+
      CASE middle_name WHEN NULL THEN '' ELSE middle_name END AS Full_Name
      FROM contacts

      Does this help?


      Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


      Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.

      V 1 Reply Last reply
      0
      • C Colin Angus Mackay

        viettho wrote: why is that happening? Because interaction with null produces null. e.g. 1 + null = null viettho wrote: Could someone help me how to make that work? Yes.

        SELECT CASE first_name WHEN NULL THEN '' ELSE first_name + ' ' END +
        CASE last_name WHEN NULL THEN '' ELSE last_name + ' ' END+
        CASE middle_name WHEN NULL THEN '' ELSE middle_name END AS Full_Name
        FROM contacts

        Does this help?


        Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


        Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.

        V Offline
        V Offline
        viettho
        wrote on last edited by
        #3

        it really does help. and I just found another way of doing that: select isnull(first_name,'')+isnull(last_name,'')+isnull(middle_name,'') from contacts

        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