Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:28:28+00:00 2026-06-04T16:28:28+00:00

I am developing a T-SQL query in SSMS 2008 R2 which returns one line

  • 0

I am developing a T-SQL query in SSMS 2008 R2 which returns one line only. But the problem is that in this one line there are four fields which I instead want to be unique rows. For example, my output line looks like:

Col. 1   Col. 2   Col. 3   Col. 4
xxxx     yyyy     zzzz     aaaa

Instead, I want this to look like:

Question    Answer
Col. 1      xxxx
Col. 2      yyyy
Col. 3      zzzz
Col. 4      aaaa

I have tried using the UNPIVOT operator for this, but it is not doing the above. How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:28:30+00:00Added an answer on June 4, 2026 at 4:28 pm

    You should be able to use UNPIVOT for this:

    Here is a static pivot where you hard code in the values of the columns:

    create table t1
    (
        col1 varchar(5),
        col2 varchar(5),
        col3 varchar(5),
        col4 varchar(5)
    )
    
    insert into t1 values ('xxxx', 'yyyy', 'zzzz', 'aaaa')
    
    select question, answer
    FROM t1
    unpivot
    (
        answer
        for question in (col1, col2, col3, col4)
    ) u
    
    drop table t1
    

    Here is a SQL Fiddle with a demo.

    but you can also use a Dynamic Unpivot:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX);
    
    select @cols = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('t1') and
                   C.name like 'Col%'
             for xml path('')), 1, 1, '')
    
    set @query = 'SELECT question, answer
                from t1
                unpivot 
                (
                   answer
                   for question in (' + @cols + ')
                ) p '
    
    execute(@query)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a SQL query that currently works for one group/mode combination. But
I'm developing an ASP.NET app (C#) that connect to SQL Server 2008 using ADO.NET
I'm developing a web app and currently using sql server 2008 for it. But,
I am using SQL Server 2008 and developing a project which is in maintenance
I am developing one web application using asp.net 2005 and Microsoft SQL Server 2008
I am writing a T-SQL Query, I am developing the e-commerce website, in that
I'm not sure if this is possible from with a SQL query, but I'll
I'm developing an Android application that makes a Query using Activity.managedQuery() , which takes
I'm having trouble wrapping my mind around developing this SQL query. Given the following
I am developing a C#/SQL ASP.NET web application in VS 2008. Currently I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.