drmaa
drmaa

Reputation: 3684

How to split a single column into multiple columns in SQL Server select statement?

I am new to SQL Server . I have a single long column with names starting from a, b, c and d. I want to show them in separate columns of NameA, NameB, NameC and NameD. I tried union but it shows in one column. I tried case but I dont know how to use it. Please help.

Existing column

names
A1
B1
A2
C1
A3
A4

A_names| B_names | C_names
A1     | B1      | C1
A2
A3
A4

Upvotes: 1

Views: 24269

Answers (5)

wvdz
wvdz

Reputation: 16651

I don't think this is something that should be solved in SQL. It's a representational thing that should probably be done in the application.

However, if you insist to use SQL for this, this is how you could do it. The main problem with this query is that the ROW_NUMBER function will be quite bad for performance.

WITH nameA
(
   SELECT name, ROW_NUMBER() OVER(ORDER BY name) AS rn
   FROM t1
   WHERE name LIKE 'a%'
), nameB AS
(
   SELECT name, ROW_NUMBER() OVER(ORDER BY name) AS rn
   FROM t1
   WHERE name LIKE 'b%'
)
SELECT name FROM nameA
FULL OUTER JOIN nameB
ON nameA.rn = nameB.rn
ORDER BY nameA.rn,nameB.rn;

Upvotes: 3

A  ツ
A ツ

Reputation: 1267

just for fun and curious why you want that:

select * 
from 
( select idx = left(names,1)
       , names
       , rn = row_number() over (partition by left(names,1) order by names) 
  from 
  ( values ('A1'),('B1'),('A2'),('C1'),('A3'),('A4'),('B2')) 
  v(names)
) dat
pivot 
( max(names)
  for idx in ([A],[B],[C],[D])
) p              

http://sqlfiddle.com/#!6/9eecb/4013/0

Upvotes: 4

Dan Field
Dan Field

Reputation: 21661

This isn't really the way relational databases work. When you have data in the same row, it's supposed to be related in some way - a common ID, at the least. What is it that would connect the person whose name happens to begin with A to the person whose name happens to begin with B? Why would you ever want the RDBMS to make such an arbitrary connection?

If you have a requirement to display users in such a way, you'd just want to write several queries and have your presentation layer deal with laying them out properly, e.g.

SELECT name FROM users WHERE name LIKE 'a%'
SELECT name FROM users WHERE name LIKE 'b%'
SELECT name FROM users WHERE name LIKE 'c%'
etc...

The presentation layer could run each query and then populate a table appropriately. Even better would be to have the presentation layer just run this query:

SELECT name FROM users

And then appropriately sort and display the data, which is probably going to be less expensive than multiple scans on your users table by SQL Server.

Upvotes: 0

Kami
Kami

Reputation: 365

Look at this post, it is very close to your problem. Itzik Ben-Gan | SQL Server Pro

Upvotes: 0

Marcelo
Marcelo

Reputation: 429

You can use CASE (https://msdn.microsoft.com/en-us/library/ms181765.aspx)

This Query should work:

SELECT 
    CASE WHEN users.name like 'a%' THEN users.name  ELSE NULL END AS NameA,
    CASE WHEN users.name like 'b%' THEN users.name  ELSE NULL END AS NameB,
    CASE WHEN users.name like 'c%' THEN users.name  ELSE NULL END AS NameC,
    CASE WHEN users.name like 'd%' THEN users.name  ELSE NULL END AS NameD
FROM users

Upvotes: 1

Related Questions