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

  • Home
  • SEARCH
  • 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 392001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:06:58+00:00 2026-05-12T16:06:58+00:00

Using SQL, how do I convert a single row table like this… Firstname Surname

  • 0

Using SQL, how do I convert a single row table like this…

Firstname Surname Address1        City   Country
--------- ------- --------------- ------ -------
Bob       Smith   101 High Street London UK

…to a table of name-value pairs like this:

Name      Value
--------- -------
Firstname   Bob
Surname     Smith
Address1    101 High Street
City        London
Country     UK

This script will create the original table:

create table #OriginalTable (Firstname varchar(10), Surname varchar(10), 
Address1 varchar(50), City varchar(10), Country varchar(10))
insert into #OriginalTable 
select 
'Bob' Firstname, 
'Smith' Surname, 
'101 High Street' Address1, 
'London' City, 
'UK' Country

I’m after a generic solution that does not depend on the columns names always being what they are in the example.

EDIT:
I’m using SQL Server 2005.
The solution I’m after is the SQL script to convert this data into a name-value pair table

ANSWER:
Using the answer that I accepted as the answer, this is what I’ve used:

select   
result.Name, 
result.Value    
from   
  (select
    convert(sql_variant,FirstName) AS FirstName,
    convert(sql_variant,Surname) AS Surname,
    convert(sql_variant,Address1) AS Address1,
    convert(sql_variant,City) AS City,
    convert(sql_variant,Country) AS Country
    from #OriginalTable) OriginalTable
  UNPIVOT (Value For Name In (Firstname, Surname, Address1, City, Country)) as result
  • 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-05-12T16:06:59+00:00Added an answer on May 12, 2026 at 4:06 pm

    Basically you have two problems – to UNPIVOT, the data types have to be conformed. The other problem is that the number of columns is unknown. You want to reach something of the form:

    WITH    conformed
          AS ( SELECT   CONVERT(VARCHAR(255), [Firstname]) AS [Firstname],
                        CONVERT(VARCHAR(255), [Surname]) AS [Surname],
                        CONVERT(VARCHAR(255), [Address1]) AS [Address1],
                        CONVERT(VARCHAR(255), [City]) AS [City],
                        CONVERT(VARCHAR(255), [Country]) AS [Country]
               FROM     so1526080
             )
    SELECT  ColumnKey,
            ColumnValue
    FROM    conformed UNPIVOT ( ColumnValue FOR ColumnKey IN ( [Firstname], [Surname], [Address1], [City], [Country] ) ) AS unpvt
    

    So using a dynamic SQL PIVOT using metadata (you might need to fix this up with TABLE_SCHEMA, etc):

    DECLARE @table_name AS SYSNAME
    SET @table_name = 'so1526080'
    DECLARE @conform_data_type AS VARCHAR(25)
    SET @conform_data_type = 'VARCHAR(255)'
    
    DECLARE @column_list AS VARCHAR(MAX)
    DECLARE @conform_list AS VARCHAR(MAX)
    
    SELECT  @conform_list = COALESCE(@conform_list + ', ', '') + 'CONVERT('
            + @conform_data_type + ', ' + QUOTENAME(COLUMN_NAME) + ') AS '
            + QUOTENAME(COLUMN_NAME),
            @column_list = COALESCE(@column_list + ', ', '')
            + QUOTENAME(COLUMN_NAME)
    FROM    INFORMATION_SCHEMA.COLUMNS
    WHERE   TABLE_NAME = @table_name
    
    DECLARE @template AS VARCHAR(MAX)
    
    SET @template = '
    WITH    conformed
              AS ( SELECT  {@conform_list}
                   FROM     {@table_name}
                 )
        SELECT  ColumnKey,
                ColumnValue
        FROM    conformed UNPIVOT ( ColumnValue FOR ColumnKey IN ( {@column_list} ) ) AS unpvt
        '
    
    DECLARE @sql AS VARCHAR(MAX)
    SET @sql = REPLACE(REPLACE(REPLACE(@template, '{@conform_list}', @conform_list),
                               '{@column_list}', @column_list), '{@table_name}',
                       @table_name)    
    
    PRINT @sql
    EXEC ( @sql
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's something about asking a question that gets the brain… May 12, 2026 at 10:12 pm
  • Editorial Team
    Editorial Team added an answer Can you set the background color in an event handler,… May 12, 2026 at 10:12 pm
  • Editorial Team
    Editorial Team added an answer Reading the registry is pretty straightforward. The Microsoft.Win32 namespace has… May 12, 2026 at 10:12 pm

Related Questions

Using SQL, how do I convert a single row table like this... Firstname Surname
I'm trying to call a stored procedure (on a SQL 2005 server) from C#,
Does anyone know how to pass a several bytes into a Binary (or varbinary)
I'm looking for possible ways to persist the following classes. Subsonic SimpleRepository looks like
I'm still learning about REST and, in my test, came up with this scenario

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.