0

I have searched SQL Server : rows into columns for splitting row values into columns and it did not help me

I have sample data

Col1
0183125031218
13020690831
79100146868
0183225031138
13010690834
79100145497

Expected Output:

Col1             Col2             Col3 
0183125031128    13020690831      79100146868
0183225031138    13010690834      79100145497

My Attempt:

I have tried with Row_number fucntion, but did not get required output. As i need a conditional splitting by left(col,2) = 01 to first column and left(col,2)=13 to second column. Based on first column , row number can be allocated. There is no fixed length on any row data

Apreciate any help or suggestions

13
  • and col3 should have all the rest of the numbers? Apr 11, 2018 at 10:26
  • @ZoharPeled Col3 based on left(col,2) = 79
    – Hadrian
    Apr 11, 2018 at 10:27
  • What defines the relationship between 0183125031128 and 13020690831? Is it simply that they are the lowest numbers in their respective groups? If the value 0183125031217 were to be added, would that the related to 13020690831 instead?
    – Thom A
    Apr 11, 2018 at 10:32
  • @larnu There is no relationship between them, it is simply lowest number as identifier for the groups
    – Hadrian
    Apr 11, 2018 at 10:37
  • I think you need to reconsider your approach if there is no relation between the columns for a certain record. You might want to consider putting the values related into a seperate table. Apr 11, 2018 at 10:47

4 Answers 4

2

Try the following query

-- test data
CREATE TABLE TestData(Col1 varchar(20))

INSERT TestData(Col1)VALUES
('0183125031218'),
('13020690831'),
('79100146868'),
('0183225031138'),
('1301069083'),
('79100145497')

-- query
SELECT
  COALESCE(q1.N,q2.N,q3.N) RowNum,
  q1.Col1,
  q2.Col1 Col2,
  q3.Col1 Col3
FROM
  (
    SELECT ROW_NUMBER()OVER(ORDER BY Col1) N,Col1
    FROM TestData
    WHERE Col1 LIKE '01%'
  ) q1
FULL JOIN
  (
    SELECT ROW_NUMBER()OVER(ORDER BY Col1) N,Col1
    FROM TestData
    WHERE Col1 LIKE '13%'
  ) q2
ON q2.N=q1.N
FULL JOIN
  (
    SELECT ROW_NUMBER()OVER(ORDER BY Col1) N,Col1
    FROM TestData
    WHERE Col1 LIKE '79%'
  ) q3
ON q3.N=COALESCE(q1.N,q2.N)

And a variant with PIVOT

SELECT *
FROM
  (
    SELECT
      LEFT(Col1,2) RowType,
      ROW_NUMBER()OVER(PARTITION BY LEFT(Col1,2) ORDER BY Col1) N,
      Col1
    FROM TestData
  ) q PIVOT(MAX(Col1) FOR RowType IN([01],[13],[79])) p
1
  • Thats Perfect thanks, Only amedment with pivot as it is returning lot of rows with null values , i will eliminate null values with cte . Thanks
    – Hadrian
    Apr 11, 2018 at 10:51
2

How about using a CTE to assign the row number and grouping?

CREATE TABLE #Sample (Col1 varchar(50));
GO
INSERT INTO #Sample
VALUES ('0183125031218'),
       ('13020690831'),
       ('79100146868'),
       ('0183225031138'),
       ('1301069083'),
       ('79100145497');
GO

SELECT *
FROM #SAmple;

WITH Grp AS(
    SELECT Col1,
           LEFT(Col1,2) AS Grp,
           ROW_NUMBER() OVER (PARTITION BY LEFT(Col1,2) ORDER BY Col1) AS RN
    FROM #Sample)
SELECT G1.Col1 AS Col1,
       G2.Col1 AS Col2,
       G3.Col1 AS Col3
FROM Grp G1
     JOIN Grp G2 ON G1.RN = G2.RN
                AND G2.Grp = '13'
     JOIN Grp G3 ON G1.RN = G3.RN
                AND G3.Grp = '79'
WHERE G1.Grp = '01';
GO

DROP TABLE #Sample;

This makes a more concise/shorter query than using the UNIONs in leran2002's answer.

0
1

Try this. The idea behind is to split the table into three columns based on given condition, then join them back based on row_number() without any ordering, as you told there's no relation between particular records.

declare @table table(col1 varchar(100))
insert into @table values
('0183125031218'),
('13020690831'),
('79100146868'),
('0183225031138'),
('1301069083'),
('79100145497')

select [col1],[col2],[col3] from
--IMPORTANT: here should go the query, that will have the most records!!!
(select col1 [col1], ROW_NUMBER() over (order by (select null)) [rn1] from @table where LEFT(col1, 2) = '01') [c1]
left join
(select col1 [col2], ROW_NUMBER() over (order by (select null)) [rn2] from @table where LEFT(col1, 2) = '13') [c2]
on [rn1]=[rn2] left join
(select col1 [col3], ROW_NUMBER() over (order by (select null)) [rn3] from @table where not LEFT(col1, 2) in ('01','13')) [c3]
on [rn1]=[rn3]
0

Just the same as Larnu (+1)
I just wanted to test it

declare @T TABLE (col varchar(50));
INSERT INTO @T
VALUES ('0183125031218'),
       ('13020690831'),
       ('79100146868'),
       ('0183225031138'),
       ('13010690834'),
       ('79100145497');
SELECT *
FROM @T;

WITH cte AS
( SELECT col, LEFT(col,2) AS grp, 
         ROW_NUMBER() OVER (PARTITION BY LEFT(col,2) ORDER BY col) AS rn
    FROM @T  
    where LEFT(col,2) in ('01', '13', '79')
)
SELECT G1.col AS Col1,
       G2.col AS Col2,
       G3.col AS Col3
FROM cte G1
JOIN cte G2 ON G1.RN = G2.RN
           and G1.Grp = '01'
           AND G2.Grp = '13'
JOIN cte G3 ON G1.RN = G3.RN
           AND G3.Grp = '79'
order by G1.col, G2.col, G3.col;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.