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 8981139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:15:33+00:00 2026-06-15T20:15:33+00:00

Suppose I have this table: Create table test(a int, b int, c int, d

  • 0

Suppose I have this table:

Create table test(a int, b int, c int, d int)

I can write simply ‘select * from test’ to get the following first record:

A    B    C    D  
1    2    3    4

But instead, I want it like this (Four rows for single record):

A: 1  
B: 2  
C: 3  
D: 4

Can someone help me in doing this?

This is required to view a single record on remote desktop system which is extremely slow, and horizontal scrolling on it sucks, and has 800 columns in it. So I need to see the format of data from a single record.

  • 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-15T20:15:35+00:00Added an answer on June 15, 2026 at 8:15 pm

    You can use the UNPIVOT function to do this, the version below concatenates the column name and value together, but you can always display them as separate columns:

    select col+':'+cast(value as varchar(10)) col
    from test
    unpivot
    (
      value
      for col in (A, B, C, D)
    ) unpiv
    

    See SQL Fiddle with Demo

    The above works great if you have a known number of columns, but if you have 800 columns that you want to transform, you might want to use dynamic sql to perform this:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @colsUnpivot = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')
    
    set @query 
      = 'select col+'':''+cast(value as varchar(10)) col
         from test
         unpivot
         (
           value
           for col in ('+ @colsunpivot +')
         ) u'
    
    exec(@query)
    

    See SQL Fiddle with Demo

    Note: when using UNPIVOT the datatypes of all of the columns that need to be transformed must be the same. So you might have to cast/convert data as needed.

    Edit #1, since your datatypes are different on all of your columns and you need to unpivot them, then you can use the following code.

    The first piece get the list of columns that you want to unpivot dynamically:

    select @colsUnpivot = stuff((select ','+quotename(C.name)
                 from sys.columns as C
                 where C.object_id = object_id('test')
                 for xml path('')), 1, 1, '')
    

    The second piece gets the same list of columns but wraps each column in a cast as a varchar:

    select @colsUnpivotCast = stuff((select ', cast('+quotename(C.name)+' as varchar(50)) as '+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')
    

    Then your final query will be:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @colsUnpivotCast AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    
    select @colsUnpivot = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')
    
    select @colsUnpivotCast = stuff((select ', cast('+quotename(C.name)+' as varchar(50)) as '+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')
    
    
    set @query 
      = 'select col+'':''+value col
         from
        (
          select '+@colsUnpivotCast+'
          from test
        ) src
         unpivot
         (
           value
           for col in ('+ @colsunpivot +')
         ) u'
    
    
    exec(@query)
    

    See SQL Fiddle with Demo

    The UNPIVOT function is performing the same process as a UNION ALL which would look like this:

    select col+':'+value as col
    from
    (
      select A value, 'A' col
      from test
      union all
      select cast(B as varchar(10)) value, 'B' col
      from test
      union all
      select cast(C as varchar(10)) value, 'C' col
      from test
      union all
      select cast(D as varchar(10)) value, 'D' col
      from test
    ) src
    

    See SQL Fiddle with Demo

    The result of all of the queries is the same:

    |    COL |
    ----------
    |    A:1 |
    | B:2.00 |
    |    C:3 |
    |    D:4 |
    

    Edit #2: using UNPIVOT strips out any of the null columns which could cause some data to drop. If that is the case, then you will want to wrap the columns with IsNull() to replace the null values:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @colsUnpivotCast AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    
    select @colsUnpivot = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')
    
    select @colsUnpivotCast = stuff((select ', IsNull(cast('+quotename(C.name)+' as varchar(50)), '''') as '+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('test')
             for xml path('')), 1, 1, '')
    
    
    set @query 
      = 'select col+'':''+value col
         from
        (
          select '+@colsUnpivotCast+'
          from test
        ) src
         unpivot
         (
           value
           for col in ('+ @colsunpivot +')
         ) u'
    
    
    exec(@query)
    

    See SQL Fiddle with Demo

    Replacing the null values, will give a result like this:

    |    COL |
    ----------
    |    A:1 |
    | B:2.00 |
    |     C: |
    |    D:4 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
Suppose, we have a test table in MySql: CREATE TABLE `test_table` ( `_id` INT(10)
Suppose I have the following tables CREATE TABLE plugins ( id int primary key,
Let's suppose I have the following table (let's call it my_table ): CREATE TABLE
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose I have this table: drop table if exists t_user; CREATE TABLE t_user (usr
Suppose I have this code create temporary table somedata (a integer); insert into somedata
I have a table like this: create table t1 { person_id int, item_name varchar(30),
Suppose I have this table parent | child 1 2 1 3 2 4

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.