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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:32:51+00:00 2026-06-01T03:32:51+00:00

I have a data row with columns A, B, C, D, E, F… How

  • 0

I have a data row with columns A, B, C, D, E, F…
How could I write a sql select to have the result:

Col1, Col2
A,    B
C,    D
E,    F
...

EDIT:
There will be only one row and dynamic number of columns. And I am not allowed to use information_schema and sys tables. Also would be interesting to know how to do it for entire table (multiple A,B,C,D,E.. rows), not the only row.

  • 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-01T03:32:53+00:00Added an answer on June 1, 2026 at 3:32 am

    If you know the list of columns ahead of time, it should be as simple as unioning each group of two columns together:

    ;with Data as (
        select 'A' as A, 'B' as B, 'C' as C, 'D' as D, 'E' as E, 'F' as F
    )
    select A,B from Data union
    select C,D from Data union
    select E,F from Data
    

    If the list of columns is dynamic, you will need Dynamic SQL to process. Here’s a fully-working example using a test table with 11 columns:

    create table Test (id01 int,id02 int,id03 int,id04 int,id05 int,id06 int,id07 int,id08 int,id09 int,id10 int,id11 int);
    insert Test values (1,2,3,4,5,6,7,8,9,10,11);
    
    declare @select varchar(max)
    ;with ColumnInfo as (
        select
            row_number() over (order by column_name) - 1 as rownumber,
            column_name
        from information_schema.columns
        where table_name = 'Test'
    )
    select
        @select = coalesce(@select + ' union ' + char(13) + char(10), '') +
            'select ' + max(case when rownumber % 2 = 0 then column_name end) +
            ',' + coalesce(max(case when rownumber % 2 = 1 then column_name end), 'NULL') +
            ' from test'
    from
        ColumnInfo
    group by
        rownumber / 2
    
    exec(@select)
    

    Sample Input:

    id01 id02 id03 id04 id05 id06 id07 id08 id09 id10 id11
    1    2    3    4    5    6    7    8    9    10    11
    

    Sample Output:

    id01 id02
    1    2
    3    4
    5    6
    7    8
    9    10
    11   NULL
    

    Live Demonstration: http://sqlfiddle.com/#!3/d0507/1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataRow with columns col1 and col2. Each column has a value:
I have a data frame with 50000 rows and 200 columns. There are duplicate
I have some data with one row of headers and one or more rows
I have a data table filled with text in each table row. How do
I have CSV data loaded into a multidimensional array. In this way each row
I have this JavaScript code: for (var idx in data) { var row =
I have a row of data that I need to modify in a database,
I have a C# console app App1 that reads a row of data from
The javascript code I now have can toggle a single row of data. Whether
Currently I have a a text file with data at the first row is

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.