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. Concatenating Columns in query

Concatenating Columns in query

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadmin
6 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.
  • A Offline
    A Offline
    anujose
    wrote on last edited by
    #1

    Hi, i am using sql server 2005 and i am writing one query to concatename 3 column values.. SELECT distinct(P.firstname + ' ' + P.middlename + ' ' + P.lastname) AS UserName ,P.Id as UserId,G.Id,G.Fkgroupid FROM c_Groupmember G,c_Person P where P.Id=G.FKUserid and G.FKgroupId=" + Id + " and P.Status=0 order by P.firstName asc it will disply the username if first name and lastname is there .if there is only firstname .then i won't display any value?.... what is the solution for this ?.in query itself i need to get all the usernames. with regards anu

    M J P 3 Replies Last reply
    0
    • A anujose

      Hi, i am using sql server 2005 and i am writing one query to concatename 3 column values.. SELECT distinct(P.firstname + ' ' + P.middlename + ' ' + P.lastname) AS UserName ,P.Id as UserId,G.Id,G.Fkgroupid FROM c_Groupmember G,c_Person P where P.Id=G.FKUserid and G.FKgroupId=" + Id + " and P.Status=0 order by P.firstName asc it will disply the username if first name and lastname is there .if there is only firstname .then i won't display any value?.... what is the solution for this ?.in query itself i need to get all the usernames. with regards anu

      M Offline
      M Offline
      M LN Rao
      wrote on last edited by
      #2

      Rewrite query as SELECT distinct( COALESEC(P.firstname,P.firstname,'') + ' ' + COALESEC(P.middlename,P.middlename,'') + ' ' + COALESEC(P.lastname,P.lastname,'')) AS UserName ,P.Id as UserId,G.Id,G.Fkgroupid FROM c_Groupmember G,c_Person P where P.Id=G.FKUserid and G.FKgroupId=" + Id + " and P.Status=0 order by P.firstName asc for more information see COALESCE function documentation.

      Intelligence is measured by common sense not by how many scholarly books you read.

      A 1 Reply Last reply
      0
      • M M LN Rao

        Rewrite query as SELECT distinct( COALESEC(P.firstname,P.firstname,'') + ' ' + COALESEC(P.middlename,P.middlename,'') + ' ' + COALESEC(P.lastname,P.lastname,'')) AS UserName ,P.Id as UserId,G.Id,G.Fkgroupid FROM c_Groupmember G,c_Person P where P.Id=G.FKUserid and G.FKgroupId=" + Id + " and P.Status=0 order by P.firstName asc for more information see COALESCE function documentation.

        Intelligence is measured by common sense not by how many scholarly books you read.

        A Offline
        A Offline
        anujose
        wrote on last edited by
        #3

        it is showing an error Msg 195, Level 15, State 10, Line 1 'COALESEC' is not a recognized built-in function name.

        A 1 Reply Last reply
        0
        • A anujose

          it is showing an error Msg 195, Level 15, State 10, Line 1 'COALESEC' is not a recognized built-in function name.

          A Offline
          A Offline
          anujose
          wrote on last edited by
          #4

          ya.i got .it is 'COALESCE'. Not 'COALESEC'

          1 Reply Last reply
          0
          • A anujose

            Hi, i am using sql server 2005 and i am writing one query to concatename 3 column values.. SELECT distinct(P.firstname + ' ' + P.middlename + ' ' + P.lastname) AS UserName ,P.Id as UserId,G.Id,G.Fkgroupid FROM c_Groupmember G,c_Person P where P.Id=G.FKUserid and G.FKgroupId=" + Id + " and P.Status=0 order by P.firstName asc it will disply the username if first name and lastname is there .if there is only firstname .then i won't display any value?.... what is the solution for this ?.in query itself i need to get all the usernames. with regards anu

            J Offline
            J Offline
            Joe 2
            wrote on last edited by
            #5

            anujose wrote:

            ?.in query itself i need to get all the usernames

            Better way of working on queries will be putting them up in a stored proc, to avoid sql injection attacks.[^]

            1 Reply Last reply
            0
            • A anujose

              Hi, i am using sql server 2005 and i am writing one query to concatename 3 column values.. SELECT distinct(P.firstname + ' ' + P.middlename + ' ' + P.lastname) AS UserName ,P.Id as UserId,G.Id,G.Fkgroupid FROM c_Groupmember G,c_Person P where P.Id=G.FKUserid and G.FKgroupId=" + Id + " and P.Status=0 order by P.firstName asc it will disply the username if first name and lastname is there .if there is only firstname .then i won't display any value?.... what is the solution for this ?.in query itself i need to get all the usernames. with regards anu

              P Offline
              P Offline
              pmpdesign
              wrote on last edited by
              #6

              Try SET CONCAT_NULL_YIELDS_NULL OFF before you begin the query. Works in 2000, not usre about 2005.

              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