Home Forums SQL Server 2005 SQL Server Newbies Generate two columns from single column with alternating data RE: Generate two columns from single column with alternating data

  • So, using this sample data: -

    SELECT ID, Val

    INTO MyInputdata

    FROM (SELECT 1,'John Smith' UNION ALL

    SELECT 2,'9/13/1961' UNION ALL

    SELECT 3,'Phony Persson' UNION ALL

    SELECT 4,'2/24/1943' UNION ALL

    SELECT 5,'Doc Galacawicz' UNION ALL

    SELECT 6,'11/11/1999')a(ID, Val);

    Anything wrong with just doing this: -

    SELECT

    MAX(CASE WHEN pos % 2 = 1 THEN Val ELSE NULL END),

    MAX(CASE WHEN pos % 2 = 0 THEN Val ELSE NULL END)

    FROM (SELECT ROW_NUMBER() OVER(ORDER BY ID), Val

    FROM MyInputdata

    )a(pos,Val)

    GROUP BY (pos + 1) / 2;


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/