Home Forums SQL Server 7,2000 T-SQL How to convert vertical records to horizontal? RE: How to convert vertical records to horizontal?

  • Frank,

    Don't be so quick to give up on the data layer.  I had a similar challenge... I've posted ALL the code including the example data and table creation.  The actual code that does the work you are looking for is very short.  If you don't need the delimiter, set the delimiter to '' and you'll be OK.

    --Jeff Moden

    Here's the code...

    SET NOCOUNT ON

    GO

    /*===== SETUP DEMONSTRATION FOR IMPROVED MAKEMODEL PROBLEM =====*/

    ----- DROP MAKEMODEL TABLE IF IT EXISTS

    IF EXISTS

     (SELECT * FROM SYSOBJECTS WHERE [NAME] = 'MakeModel')

     DROP TABLE dbo.MakeModel

    GO

    ----- IF THE NEW FUNCTION EXISTS, DROP IT -----

    IF EXISTS

     (SELECT * FROM SYSOBJECTS WHERE [NAME] = 'fn_DelimitRow')

     DROP FUNCTION dbo.fn_DelimitRow

    GO

    ----- CREATE THE MAKEMODEL TABLE -----

    CREATE TABLE dbo.MakeModel

     (

     [ID]    [int] IDENTITY (1, 1) NOT NULL ,

     [MAKE]  [varchar]      (10)       NULL ,

     [MODEL] [varchar]      (20)       NULL

    &nbsp ON [PRIMARY]

    GO

    ----- POPULATE THE MAKE MODEL TABLE WITH EXAMPLE DATA -----

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('FORD','MUSTANG')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('CHEVY','MALIBU')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('CHEVY','NOVA')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('LINCOLN','CONTINENTAL')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('FORD','NAVIGATOR')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('FORD','F150 TRUCK')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('GMC','SIERRA')

    INSERT INTO MakeModel (MAKE,MODEL)

           VALUES ('CHEVY','S10')

    GO

    ----- DISPLAY CONTENTS OF TABLE TO VERIFY

    SELECT *

      FROM MakeModel

    GO

    /*===== CREATE THE NEW FUNCTION =====*/

    ----- Could parameterize more for table and column names

    --    and use with dynamic SQL to run

    CREATE FUNCTION fn_DelimitRow (@Make VARCHAR(50),@Delim VARCHAR(10))

           RETURNS VARCHAR(8000)

    AS

    BEGIN

     DECLARE @info  VARCHAR(8000)

     

            --  Next line is "JBM PFM!"

     SELECT  @info = COALESCE(@Info + @Delim, '') + RTRIM(MODEL)

       FROM  MakeModel

      WHERE  MAKE=@Make

      ORDER BY  MODEL

     RETURN (@Info)

    END

    GO

    /*===== NOW, DEMO NEW MAKEMODEL PROBLEM SOLUTION =====*/

    ----- No temp or working tables and no cursors!

    ----- Can pass any 1-10 character delimiter in single quotes

    SELECT  DISTINCT MAKE, dbo.fn_DelimitRow(MAKE,', ') AS MODELS

      FROM  MakeModel

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions