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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:59:43+00:00 2026-05-13T22:59:43+00:00

I have a table as below Name Priority Date ————————- A 2 d1 B

  • 0

I have a table as below

Name    Priority    Date    
-------------------------
A         2          d1
B         3          d2

How to write a query to achieve the below output

ColumnNames   d1      d2
--------------------------
 Name         A       B
 Priority     2       3

Thanks

  • 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-13T22:59:43+00:00Added an answer on May 13, 2026 at 10:59 pm

    Here is the solution I promised:

    EDIT: I modified my answer to answer additional questions by the OP.

    A few things to note:

    • The STUFF function: This is used to convert the XML string into a regular string (and to remove the first comma)
    • Group By (I stole this from OMG Ponies): You need to do this to ensure you don’t have any duplicate dates
    • Make sure there aren’t too many dates in the table before you run this. Too many columns can be a problem
    • NVARCHAR: I used this instead of VARCHAR for the @sql variable because sp_ExecuteSQL requires it.
    • CONVERT(VARCHAR, DateColumn, 101): I did this because unless you convert the date to a string, this wont work. 101 results in this: mm/dd/yyyy but you can use whatever you need (make sure the 2 times it is used in this script matches)
    • In order for this to work for multiple columns, you must first use UNPIVOT and convert all columns to the same datatype (more information below the code)
    • It is important to note that in order to concatenate strings, they must be of the same datatype, with the same size (in my case, they are both NVARCHAR(MAX))

    Read this page for more information on converting dates to strings.

    With that said, here is the code:

    -- table with multiple columns
    CREATE TABLE #TBL ( 
        NameColumn VARCHAR(10), 
        PriorityColumn INT,
        AnotherColumn FLOAT,
        DateColumn DATETIME 
    )
    
    
    -- Insert the test data
    INSERT INTO #TBL VALUES ('a', 1, 7.2, '1/1/2000')
    INSERT INTO #TBL VALUES ('a', 2, 8.9, '1/2/2000')
    INSERT INTO #TBL VALUES ('a', 2, 53.2, '1/3/2000')
    INSERT INTO #TBL VALUES ('a', 3, 9.12, '1/4/2000')
    INSERT INTO #TBL VALUES ('b', 2, 1.26, '1/1/2001')
    
    
    DECLARE
        @sql NVARCHAR(max),
        @dates NVARCHAR(max)
    
    
    -- I separated this to make the code easier to read
    SET @dates = STUFF(
        (
            SELECT N',[' + CONVERT(VARCHAR, DateColumn, 101) + ']' AS [text()]
            FROM #TBL
            GROUP BY DateColumn
            ORDER BY DateColumn
            FOR XML PATH('')
        ), 1, 1, N''
    )
    
    
    -- I will break this part of the code up below
    SET @sql = N'SELECT
        *
    FROM (
        SELECT
            ColumnName,
            ColumnValue,
            CONVERT(VARCHAR, DateColumn, 101) AS DateString
        FROM (
            SELECT
                CAST(NameColumn AS VARCHAR(100)) AS NameColumn,
                CAST(PriorityColumn AS VARCHAR(100)) AS PriorityColumn,
                CAST(AnotherColumn AS VARCHAR(100)) AS AnotherColumn,
                DateColumn
            FROM #TBL
        ) P
        UNPIVOT (
            ColumnValue
            FOR ColumnName IN (NameColumn, PriorityColumn, AnotherColumn)
        ) UNPIV
    ) P2
    PIVOT (
        MAX(ColumnValue)
        FOR DateString IN (' + @dates + N')
    ) PIV'
    
    EXECUTE dbo.sp_ExecuteSQL @sql
    
    
    DROP TABLE #TBL
    

    Lets run through this a little

    -- I first do an UNPIVOT on all of the columns I want to pivot on, at the same time, converting them to the same datatype
    SELECT
        ColumnName,
        ColumnValue,
        CONVERT(VARCHAR, DateColumn, 101) AS DateString
    FROM (
        SELECT
            CAST(NameColumn AS VARCHAR(100)) AS NameColumn,
            CAST(PriorityColumn AS VARCHAR(100)) AS PriorityColumn,
            CAST(AnotherColumn AS VARCHAR(100)) AS AnotherColumn,
            DateColumn
        FROM #TBL
    ) P
    UNPIVOT (
        ColumnValue
        FOR ColumnName IN (NameColumn, PriorityColumn, AnotherColumn)
    ) UNPIV
    

    Once I do this, the data will look something like this:

    ColumnName  ColumnValue DateString
    ----------------------------------
    NameColumn      a       01/01/2000
    PriorityColumn  1       01/01/2000
    AnotherColumn   7.2     01/01/2000
    NameColumn      a       01/02/2000
    PriorityColumn  2       01/02/2000
    AnotherColumn   8.9     01/02/2000
    NameColumn      a       01/03/2000
    PriorityColumn  2       01/03/2000
    AnotherColumn   53.2     01/03/2000
    NameColumn      a       01/04/2000
    PriorityColumn  3       01/04/2000
    AnotherColumn   9.12     01/04/2000
    NameColumn      b       01/01/2001
    PriorityColumn  2       01/01/2001
    AnotherColumn   1.26     01/01/2001
    

    Then we can use PIVOT like this to get all the columns we need:

    PIVOT (
        MAX(ColumnValue)
        FOR DateString IN (' + @dates + N')
    ) PIV
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • 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 Let's dissect this carefully, since array-to-pointer conversions are sometimes confusing.… May 15, 2026 at 1:27 pm
  • Editorial Team
    Editorial Team added an answer Because it forces anyone who uses your header file to… May 15, 2026 at 1:27 pm
  • Editorial Team
    Editorial Team added an answer I imagine a simple way would be to do: myRtb.Text… May 15, 2026 at 1:27 pm

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.