2

What I'm trying to do is combine multiple columns into one. Say I have a table called Player, this table contains multiple columns e.g name, team, goalie, left back, right back, center back , left midfield.... and description.

The description column would contain name + team + left back , so description would be Ronaldo - Madrid - Forward. I can populate the description column when a player only has one position, but not for more than one position?

My SQL:

select 
    name, team, goalie,
    leftback, rightback, centerback,
    left midfield,
    ......,
    name + ' - ' + team + ' - '+ 
    CASE 
       WHEN goalie = 'yes' THEN 'Goalie' 
       WHEN leftback = 'yes' THEN 'Left Back'
       WHEN rightback = 'yes' THEN 'Right Back'
       WHEN centerback = 'yes' THEN 'Center Back'
       .......
    END AS Description
from 
    player

Output:

Ronaldo - Madrid - Forward

How can I get it to add more then one position, if the player has more than one?

Thanks

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
AndroidAL
  • 1,111
  • 4
  • 15
  • 35
  • have you tried using a concat over your case? – davejal Nov 20 '15 at 16:17
  • @davejal - group conconat is not a function in sql server. – Hogan Nov 20 '15 at 16:18
  • what version of sql server? – Juan Carlos Oropeza Nov 20 '15 at 16:18
  • May be helpful: http://stackoverflow.com/questions/8868604/sql-group-concat-function-in-sql-server – Dan Nov 20 '15 at 16:19
  • 3
    You really should look at normalizing this. I would have a table for players, another for positions and a bridging table for PlayerPositions. That way a player can play any number of positions. – Sean Lange Nov 20 '15 at 16:19
  • @JuanCarlosOropeza, sql server 2012 – AndroidAL Nov 20 '15 at 16:20
  • @SeanLange -- you don't mean "normalizing" – Hogan Nov 20 '15 at 16:28
  • @Hogan I don't understand your comment. I am referring to normalization. – Sean Lange Nov 20 '15 at 16:43
  • @SeanLange - No you mean 3rd normal form. This data model could be fine depending on the user's requirement. 3rd normal form is not always required or even recommended. Knee jerk 3rd normal form is a mistake in my opinion... that was my point. – Hogan Nov 20 '15 at 17:00
  • I would not say that normalizing the 3rd normal is knee jerk. Sure this model can be made to work just like anything in any level of normalized form can be made to work. That doesn't mean it is a good idea. If this were in 3NF it would be easier to work with in my opinion. – Sean Lange Nov 20 '15 at 17:15
  • @SeanLange We know nothing about this data and I don't know much about football -- but maybe this is a table of defensive players... then there would never be another position needed than these 3. This is a possible example why 3NF is not needed. – Hogan Nov 20 '15 at 17:32

2 Answers2

5
select 
   name,
   team, 
   CASE WHEN goalie = 'yes'      THEN 'Goalie - '      ELSE '' END + 
   CASE WHEN leftback = 'yes'    THEN 'LeftBack - '    ELSE '' END + 
   CASE WHEN rightback = 'yes'   THEN 'RightBack - '   ELSE '' END + 
   CASE WHEN center back = 'yes' THEN 'Center Back - ' ELSE '' END AS Description

And you have to remove the last - on description

Version for SQL Server +2012

WITH cte as (
   SELECT
      name,
      team, 
      IIF(goalie = 'yes',      'Goalie - ', '') + 
      IIF(leftback = 'yes',    'LeftBack - ', '') + 
      IIF(rightback = 'yes',   'RightBack - ', '') + 
      IIF(center back = 'yes', 'Center Back - ', '') AS Description
   FROM Players
)
SELECT name,
       team,
       CASE LEN(Description) 
          WHEN 0 THEN Description 
          ELSE LEFT(Description, LEN(Description) - 3)
       END
FROM cte

Check In the comment for a version using STUFF

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
1

I'm not a fan of the CASE statements and prefer to avoid them when I can. You could get away with something like:

select stuff (
              isnull(replace(replace(goalie,'no',''),'yes',' - Goalie'),'')
            + isnull(replace(replace(leftback,'no',''),'yes',' - Leftback'),'')
            + isnull(replace(replace(rightback,'no',''),'yes',' - Rightback'),'')
              ....
              ,1,3,''
             ) as 'Description'
  from Player

if the only values for goalie and the like are 'yes', 'no', and NULL.

Brian Stork
  • 945
  • 2
  • 8
  • 14