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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:41:19+00:00 2026-05-25T00:41:19+00:00

I need a select statement for a stored procedure with @startDate and @endDate parameters.

  • 0

I need a select statement for a stored procedure with @startDate and @endDate parameters. It needs to return a dynamic amount of columns that are labeled in a month year (“August 2011”) format based on the date range parameters.

For example, if
@startDate = 1/1/2011 and
@endDate = 3/1/2011

the result set would look something like:

column headers ---->  January 2011 | February 2011 | March 2011
rows with data ---->       123     |      3456     |    793

a function taking the datetime corresponding to the column header will be used for rows of data

Is this possible? Use pivot?
Thanks in advance for all of your responses!

  • 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-25T00:41:19+00:00Added an answer on May 25, 2026 at 12:41 am

    Assuming you don’t need to touch any tables (all data comes from the function). Note that the limit on a local NVARCHAR variable in SQL Server 2000 is 4,000 characters, so you’ll need to be careful about how long your range can be.

    DECLARE 
        @startDate SMALLDATETIME, 
        @endDate   SMALLDATETIME;
    
    SELECT 
        @startDate = '20110101', 
        @endDate   = '20110301';
    
    
    DECLARE 
        @i       INT,
        @sd      SMALLDATETIME,
        @sql     NVARCHAR(MAX),
        @column  VARCHAR(32),
        @columns NVARCHAR(4000);
    
    SELECT @i = 0, @columns = N'';
    
    WHILE @i <= DATEDIFF(MONTH, @startDate, @endDate)
    BEGIN
        SET @sd = DATEDIFF(DAY, 0, DATEADD(MONTH, @i, @startDate));
    
        SET @column = DATENAME(MONTH, @sd) 
            + ' ' + CONVERT(VARCHAR(20), YEAR(@sd));
    
        SET @columns = @columns + ',' + CHAR(13) + CHAR(10) 
            + ' [' + @column + ']' + ' = dbo.FunctionName(''' + @column + ''')';
    
        SET @i = @i + 1;
    END
    
    SET @sql = 'SELECT ' + STUFF(@columns, 1, 1, '') + ';';
    
    PRINT @sql;
    --EXEC sp_executesql @sql;
    

    In this case this yields:

    SELECT 
     [January 2011] = dbo.FunctionName('January 2011'),
     [February 2011] = dbo.FunctionName('February 2011'),
     [March 2011] = dbo.FunctionName('March 2011');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: need help with some stored procedure plz ERROR:- A SELECT statement that
I need to access the result of a stored procedure within a select statement,
I have a stored procedure that uses this select statement: SELECT dbo.VendorProgram.id, dbo.VendorProgram.CODE, dbo.Programs.ProgramName
I'm using a select statement inside a stored procedure. All I need to do
I have a rather complex SELECT statement in a stored procedure that I am
I need a stored procedure which will allow me to return sorted results based
I need a some stored procedure that called like: search('foo bar') makes a search
I need to echo a statement in stored procedure DELIMITER $$ DROP PROCEDURE IF
I have a stored procedure which need to run IN statement. I want to
i need a experts advice that i should create separate stored procedure for every

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.