You must Sign In to post a response.
  • Category: SQL Server

    How to change column name

    I want to change column name my requriment is following,

    macaddress softname
    00-15-5D-01-C8-22 windows_xp
    00-15-5D-01-C8-20 TightVNC 2.0.2
    00-15-5D-01-C8-20 VMS Windows Agent
    00-15-5D-01-C8-20 Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.17


    I want to result is

    macaddress windows_xp TightVNC 2.0.2 VMS Windows Agent
    00-15-5D-01-C8-22


    so how to do this I am new this so plz give me suggestion
    thanks,
    ms
  • #746968
    Hi, Use the below sql script

    DECLARE @ColumnName AS VARCHAR(max),
    @SQLQuery AS VARCHAR(max)

    SELECT @ColumnName = COALESCE(@ColumnName + ', ', '')
    + LTRIM(RTRIM(a.softname))
    FROM (SELECT DISTINCT softname
    FROM table_11) a

    SET @SQLQuery = 'SELECT '
    + @ColumnName
    + ' FROM (SELECT softname,macaddress FROM table_11) AS SourceTable PIVOT ( max(macaddress) FOR softname IN ('
    + @ColumnName + ') ) AS pivottable';

    EXEC(@SQLQuery)


    regards
    Siva


  • Sign In to post your comments