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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:10:29+00:00 2026-06-10T14:10:29+00:00

I have a query that returns a single row of data. Let’s say it

  • 0

I have a query that returns a single row of data. Let’s say it looks like this (with column headers).

Fruit    | Veg     | Meats   | Nuts    |
----------------------------------------
Apple      Lettuce   Veal      Almond

Instead of this, I’d like to return this single row as a two column table, with the column/field name in the leftmost column. Like so:

FieldName  | Value    |
-----------------------
Fruit        Apple
Veg          Lettuce
Meats        Veal
Nuts         Almond

Seems like there should be some non-painful way to make this happen, but if there is I can’t seem to find it. Hit a brick wall. Is this even possible?

Thanks much for any input.

EDIT: The query returns results that aren’t static, so I won’t be able to explicitly define the column headings.

EDIT 2: I’m trying to get some of these suggestions to work, and still can’t get this to function with more than one column. Here’s my code:

SELECT
  TOP 10
  FieldName,
  CAST(FieldValue as varchar(MAX)) As FieldValue
FROM Meter M
UNPIVOT([FieldValue] FOR FieldName IN ([MeterName],[MID])) UP
WHERE ClientNumber = 12300

Every time, I get the error “Msg 8167, Level 16, State 1, Line 4
The type of column “MID” conflicts with the type of other columns specified in the UNPIVOT list.”

I’ve looked this up, and it appears to be an issue related to column types/length, and no matter how I cast the FieldValue, same error. Any thoughts there?

EDIT 3: Resolution to the column conflict…

Casting results from the original table would NOT work, so I had to use a derived table and cast there so that the columns being returned to the PIVOT were all the same data type. Odd, but the only solution I could find that would run.

SELECT
  FieldName,
  CAST(FieldValue as varchar(MAX)) As FieldValue,
FROM 
(
  /* 
  Derived table so that we can control the column
  types for everything selected, because PIVOT demands
  that every pivoted column be of the same data type and
  length.
  */
  SELECT
    TOP 1
    CAST(M.MeterName as varchar(MAX)) As MeterName,
    CAST(M.MID as varchar(MAX)) As MID,
    CAST(D.DeviceType as varchar(MAX)) As DeviceType
  FROM Meter M
    INNER JOIN CurrentDevice D ON
      D.ClientNumber = M.ClientNumber AND
      D.MID = M.MID
  WHERE 
    M.ClientNumber = 12300
) T
UNPIVOT([FieldValue] FOR FieldName IN ([MeterName],[MID],[DeviceType])) UP
  • 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-10T14:10:30+00:00Added an answer on June 10, 2026 at 2:10 pm

    For SQL Server 2005+ you can use UNPIVOT:

    SELECT FieldName, Value
    FROM YourTable T
    UNPIVOT([Value] FOR FieldName IN (Fruit, Veg, Meats, Nuts)) UP
    

    Here is an SQL Fiddle for you to try.

    Updated for new requirement

    Ok, so you need dynamic SQL if you don’t know the columns of your table. First, go to this link. Then you can try the follwing:

    DECLARE @Fields NVARCHAR(MAX), @Sql NVARCHAR(MAX)
    
    SELECT @Fields = STUFF((SELECT DISTINCT ',' + QUOTENAME(Name) 
                            FROM sys.columns
                            WHERE OBJECT_ID = OBJECT_ID('YourTable')
                            FOR XML PATH('')),1,1,'')
    
    
    SET @Sql = '
    SELECT FieldName, Value
    FROM YourTable T
    UNPIVOT([Value] FOR FieldName IN ('+@Fields+')) UP
    '
    
    EXEC sp_executesql @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like this: select id , int1 , int2
I have a single column table which looks like this: Gaming | Austria |
I have a query which returns a result set which looks like this: A
I have a MySQL query that returns a single row that is a series
I have a query that I am currently getting the ORA-01427: single-row subquery returns
Ok, I have a LinqToXml query that returns a single record (correct terminology?), however,
I have this query that returns the results by ordering it by focus.name ASC.
I have a query that returns one row. However, I want to invert the
I have a query that returns a lot of data into a CSV file.
I have a query that returns data in the following form attribute value ---------

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.