procma
procma

Reputation: 1440

SQL Server columns as rows in result of query

How to get table where some columns are queried and behaves as rows?

Source table

ID | Name | Funct    | Phone1 | Phone2 | Phone3
1  | John | boss     | 112233 | 114455 | 117788
2  | Jane | manager  | NULL   | NULL   | 221111
3  | Tony | merchant | 441100 | 442222 | NULL

Wanted result

ID | Name | Funct    | Phone  | Ord
1  | John | boss     | 112233 | 1
1  | John | boss     | 114455 | 2
1  | John | boss     | 117788 | 3
2  | Jane | manager  | 221111 | 3
3  | Tony | merchant | 441100 | 1
3  | Tony | merchant | 442222 | 2

Ord is a column where is the order number (Phone1...Phone3) of the original column

EDITED:

OK, UNION would be fine when phone numbers are in separed columns, but what if the source is following (all numbers in one column)?:

ID | Name | Funct    | Phones
1  | John | boss     | 112233,114455,117788
2  | Jane | manager  | 221111
3  | Tony | merchant | 441100,442222

Here I understand, that column Ord is a non-sense (so ignore it in this case), but how to split numbers to separed rows?

Upvotes: 0

Views: 72

Answers (3)

Jithin Shaji
Jithin Shaji

Reputation: 6073

Please see the answer below,

Declare @table table
(ID int, Name varchar(100),Funct varchar(100),Phones varchar(400))

Insert into @table Values 
(1,'John','boss','112233,114455,117788'),
(2,'Jane','manager','221111' ),
(3,'Tony','merchant','441100,442222')

Select * from @table

Result:

enter image description here

Code:

Declare @tableDest table
([ID] int, [name] varchar(100),[Phones] varchar(400))

Declare @max_len int,
        @count int = 1

Set @max_len =  (Select max(Len(Phones) - len(Replace(Phones,',','')) + 1)
                From    @table)

While @count <= @max_len
begin
    Insert  into @tableDest
    Select  id,Name,
            SUBSTRING(Phones,1,charindex(',',Phones)-1)
    from    @table
    Where   charindex(',',Phones) > 0
    union   
    Select  id,Name,Phones
    from    @table
    Where   charindex(',',Phones) = 0

    Delete from @table
    Where   charindex(',',Phones) = 0

    Update  @table
    Set     Phones =  SUBSTRING(Phones,charindex(',',Phones)+1,len(Phones))
    Where   charindex(',',Phones) > 0

    Set     @count = @count + 1
End
------------------------------------------
Select  * 
from    @tableDest
Order By ID
------------------------------------------

Final Result:

enter image description here

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269753

The easiest way is to use union all:

select id, name, funct, phone1 as phone, 1 as ord
from source
where phone1 is not null
union all
select id, name, funct, phone2 as phone, 2 as ord
from source
where phone2 is not null
union all
select id, name, funct, phone3 as phone, 3 as ord
from source
where phone3 is not null;

You can write this with a cross apply as:

select so.*
from source s cross apply
     (select s.id, s.name, s.funct, s.phone1 as phone, 1 as ord union all
      select s.id, s.name, s.funct, s.phone2 as phone, 2 as ord union all
      select s.id, s.name, s.funct, s.phone3 as phone, 3 as ord
     ) so
where phone is not null;

There are also methods using unpivot and cross join/case.

Upvotes: 2

Rohin
Rohin

Reputation: 526

       SELECT CONCAT(
      'CREATE TABLE New_Table (',GROUP_CONCAT(
                            DISTINCT CONCAT(Name, ' VARCHAR(50)')
                            SEPARATOR ','),');')
      FROM
      Previous_Table
      INTO @sql;
      PREPARE stmt FROM @sql;
      EXECUTE stmt;

This query generates a row in a table from the column values of another table. This may help you

Upvotes: 0

Related Questions