Results in specific format

  • Hi,

    I have a table with one column. Now I want to display the results in a specific format.

    CREATE TABLE #TempTable

    (

    Question varchar(30)

    )

    GO

    INSERT INTO #TempTable(Question) VALUES ('First');

    GO

    INSERT INTO #TempTable(Question) VALUES ('Second');

    GO

    INSERT INTO #TempTable(Question) VALUES ('Third');

    GO

    INSERT INTO #TempTable(Question) VALUES ('Fourth');

    GO

    INSERT INTO #TempTable(Question) VALUES ('Fifth');

    GO

    INSERT INTO #TempTable(Question) VALUES ('Sixth');

    GO

    SELECT * FROM #TempTable

    --First,Second,Third,Fourth,Fifth,Sixth

    These results needs to be shown as columns.

  • Presentation is usually a job for the application layer. However, if you want to do it with T-SQL, you can do something like this:

    DECLARE @List varchar(1000)

    SET @List = ''

    SELECT @List = @List + Question + ', '

    FROM #TempTable

    SET @List = LEFT(@List, LEN(@List) - 1) --chop off the last comma

    SELECT @List AS List

    Bear in mind, though, that the results could come out in any order, and not necessarily the order in which you inserted them into the table.

    John

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply