convert a roow of data under a single column heading

  • I have a table Number with following columns

    ID               Number 1

    1

     


    Kindest Regards,

    Pavas

    Dream The Dream
    Explore the World
    Experince The Exhilaration

  • The following code can convert all columns into one for any table in the current db. The table should not contain image and timestamp columns. You can easily change the code to exclude those columns if it has.

    USE NorthWind

    GO

    DECLARE @Col nvarchar(4000)

    DECLARE @Table nvarchar(128)

    DECLARE @Delimiter nvarchar(1)

    -- Take dbo.Customers as example

    -- @Delimiter can be empty string if not required, i.e. SET @Delimiter=''

    SELECT @Col=N'',@Table='dbo.Customers',@Delimiter=';'

    SELECT @Col=@Col+'CAST(ISNULL('+name+','''') AS nvarchar(4000))+'''+@Delimiter+'''+' FROM syscolumns where ID=OBJECT_ID(@Table)

    SET @Col=LEFT(@Col,Len(@Col)-4-LEN(@Delimiter))

    --PRINT @Col

    EXEC ('SELECT '+@Col+' AS Output FROM '+@Table)

     

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

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