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

The Archive Base Latest Questions

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

I know this may sounds silly but I would like to create function that

  • 0

I know this may sounds silly but I would like to create function that will process data from tables with different size.

Lets say I have first table like so:

ID    IRR     M0    M1
----------------------
 1      0    -10     5
 2      0    -20    10
 3      0   -100   100
 4      0    -10     0

And second table like so:

ID    IRR     M0    M1    M2
----------------------------
 1      0    -10     5    60
 2      0    -20    10     0
 3      0   -100   100   400
 4      0    -10     0    10

I would like to create function that will be able to process data from both tables.

I know that first column contains ID, second IRR, rest of columns will hold cash flow for specific month.

Function should be able to process all columns instead of first 2 and store result in second column.

I know that I can get all columns from specific table with:

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.columns 
WHERE TABLE_NAME = 'First_Table'

Problems begin when I would like to create function that will return those columns as rows.

I can create function like so:

CREATE FUNCTION UnpivotRow (@TableName  varchar(50), @FromWhichColumn int, @Row int)
RETURNS @values TABLE
(
    id INT IDENTITY(0, 1),
    value DECIMAL(30, 10)
)
...

But how this function should look?

I think that ideal for this kind of processing table should look like so:

ProjectID    TimePeriod    Value
--------------------------------
        1             0      -10
        1             1        5
        2             0      -20
        2             1       10
        3             0     -100
        3             1      100
        4             0      -10
        4             1        0

I need to unpivot whole table without knowing number of columns.


EDIT:
If this can’t be done inside function, then maybe inside a procedure?

  • 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-10T04:57:31+00:00Added an answer on June 10, 2026 at 4:57 am

    This can be done using dynamic SQL to perform the UNPIVOT:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX),
        @colTP as  NVARCHAR(MAX)
    
    select @cols = stuff((select ','+quotename(C.name)
             from sys.columns as C
             where C.object_id = object_id('table1') and
                   C.name like 'M%'
             for xml path('')), 1, 1, '')
    
    
    set @query = 'SELECT id, 
                  replace(timeperiod, ''M'', '''') timeperiod, 
                  value
                  from table1
                unpivot 
                (
                   value
                   for timeperiod in (' + @cols + ')
                ) u '
    
    exec(@query)
    

    See SQL Fiddle with Demo.

    This solution would have to be placed in a stored procedure.

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

Sidebar

Related Questions

Now this may seem like a silly question, but I need to know how
I know this may sounds like a pointless question, but hear me out... I
I know this may sound silly, but I'd like to put a version in
I know this may sound silly but it seems that, my version of TortoiseSVN
I know this may seem like a math question but i just saw this
I know that this may be an amateur question but for some reason I
It may sound very silly to people who know this but I am quite
I know this may sound silly little bit, but there is no other way
I know that this may sound like blasphemy to Lisp aficionados (and other lovers
I know how silly this may sound, but is there a way to attach

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.