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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:23:39+00:00 2026-06-04T04:23:39+00:00

I have a table which currently looks a bit like this- CASEID ¦ FORMNAME

  • 0

I have a table which currently looks a bit like this-

CASEID  ¦  FORMNAME  ¦  NAME  ¦  VALUE

601554  ¦  CASEFORM  ¦  Mond  ¦  AAAA
601554  ¦  CASEFORM  ¦  Tues  ¦  BBBB
601554  ¦  CASEFORM  ¦  Wedn  ¦  CCCC
601554  ¦  CASEFORM  ¦  Thur  ¦  DDDD

I now want to create a new table in SQL that will copy the data and change the format of it entirely as follows-

CASEID  ¦  FORMNAME  ¦  Mond  ¦  Tues  ¦  Wedn  ¦  Thur

601554  ¦  CASEFORM  ¦  AAAA  ¦  BBBB  ¦  CCCC  ¦  DDDD

The original table has about 400 lines, so the new table is going to need 400 columns.

My SQL knowledge is certainly limited, but I can always blunder my way to a solution when I need one. In this case however I don’t even know where to begin. Can someone point me in the right direction?

  • 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-04T04:23:40+00:00Added an answer on June 4, 2026 at 4:23 am

    Start with this query:

    create table NewTable(CASEID int,  FORMNAME varchar(255))
    go
    
    insert into NewTable select distinct caseid,formname from OldTable
    go
    
    select  distinct 'alter table NewTable add ' +[name]+ ' varchar(255)'  from oldtable
    

    and then copy the results and run them.

    Then run this query:

    select distinct 'update NewTable set ' +Name+ ' = ''' + Value +''' where caseid = ' +cast(caseid as varchar(20))+
    ' and FORMNAME = '''+Formname+''''
    from oldtable
    

    and then copy the results and run them.

    Edit: Added automated version:

    create table NewTable(CASEID int,  FORMNAME varchar(255)) 
    go 
    insert into NewTable select distinct caseid,formname from OldTable 
    go
    DECLARE @query VARCHAR(max)
    set @query = ''
    select  @query = @query + 'alter table NewTable add ' +[name]+ ' varchar(255);'  from (select distinct name from oldtable )c
    exec (@query)
    set @query = ''
    select @query = @query+ 'update NewTable set ' +Name+ ' = ''' + Value +''' where caseid = ' +cast(caseid as varchar(20))+ ' and FORMNAME = '''+Formname+'''' 
    from  (select distinct  Name, Value, Caseid, FormName from OldTable)c
    exec (@query)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which looks a bit like this: <table> <thead> <tr> <th></th>
I have a table which looks like this: Column | Type | ----------------------+-------------------------+ ...
I have a table that looks a bit like this: user_id item_id bid_amount 5
I have a table which looks something like Event ID Date Instructor 1 1/1/2000
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
I currently have a table which only has a single editable column. I have
I have a table in MySQL 4.0 which currently has a year field as
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I'm currently dynamically building an asp:table which contains checkboxes these checkboxes have a CheckChanged
I currently have a table called paper , which holds all the information for

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.