locked
how to change column to row RRS feed

  • Question

  • User-1634604574 posted

    i have this result

    name  id

    a         1

    b         2

    c          3

    i want  convert to this result

    a

    b

    c

    1

    2

    3

    Wednesday, November 20, 2019 4:57 AM

All replies

  • User-2082239438 posted
    SELECT Name FROM tableName
    UNION ALL
    SELECT ID AS Name FROM tableName

    Wednesday, November 20, 2019 5:53 AM
  • User77042963 posted

    You should learn to put your question into script fro T-SQL question. Thanks.

    Here is a sample for you and the query:

    CREATE TABLE sample(
       name VARCHAR(1)  
      ,id   INT  
    );
    INSERT INTO sample(name,id) VALUES 
    ('a',1) 
    ,('b',2) 
    ,('c',3);
    
    Select col from sample
    Cross apply (values(name),(try_cast(id as varchar(1))) ) d(col)
    
    
    drop TABLE sample
    

    Wednesday, November 20, 2019 3:20 PM