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

  • Home
  • SEARCH
  • 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 9221239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:32:06+00:00 2026-06-18T03:32:06+00:00

I’m unable to take the 12 months back from current date in a SQL

  • 0

I’m unable to take the 12 months back from current date in a SQL statement to use with my Stimulsoft report. The sql table structure is this (figure 1), note that im brazilian so the culture is set to pt-BR (dd/mm/yyyy)

this is the SQL table

http://img534.imageshack.us/img534/1093/clipboard02xva.jpg

I want to make this, the current date for the last 12 months is 01/02/2013 so we take:

| DACP_Id | FEB 2012 | MAR 2012 | ABR 2012 ... | FEB 2013

… and all the months heading back to february 2013. The rows inside the months in the pivot table are the DACP_Value in the current month.

I’m new to SQL so I hope you can help 🙂 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-06-18T03:32:07+00:00Added an answer on June 18, 2026 at 3:32 am

    I would be inclined not to use a dynamic pivot, and to handle the headers in your application code:

    WITH Data AS
    (   SELECT  DACP_ID,
                DACP_Value,
                [MonthNum] = 12 - DATEDIFF(MONTH, DACP_Date, CURRENT_TIMESTAMP)
        FROM    T
        WHERE   DATEDIFF(MONTH, DACP_Date, CURRENT_TIMESTAMP) BETWEEN 0 AND 12
    )   
    SELECT  *
    FROM    Data
            PIVOT
            (   SUM(DACP_Value)
                FOR MonthNum IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
            ) pvt;
    

    If you really need the Columns headers as they are then use:

    DECLARE @Col NVARCHAR(MAX) = 
        (   SELECT  ', ' + QUOTENAME(CONVERT(VARCHAR, DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - (12 - Number), 0), 112)) + ' = [' + CAST(number AS VARCHAR) + ']'
            FROM    Master..spt_values
            WHERE   Type = 'P'
            AND     number BETWEEN 0 AND 12
            FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)');
    
    
    DECLARE @SQL NVARCHAR(MAX) = 
            N'WITH Data AS
            (   SELECT  DACP_ID,
                        DACP_Value,
                        [MonthNum] = 12 - DATEDIFF(MONTH, DACP_Date, CURRENT_TIMESTAMP)
                FROM    T
                WHERE   DATEDIFF(MONTH, DACP_Date, CURRENT_TIMESTAMP) BETWEEN 0 AND 12
            )   
            SELECT  DACP_ID' + @Col + '
            FROM    Data
                    PIVOT
                    (   SUM(DACP_Value)
                        FOR MonthNum IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
                    ) pvt;';
    
    EXECUTE SP_EXECUTESQL @SQL;
    

    You can change the value 112 when converting a date to a varchar to various numbers to change the format of your date in the column headers (103 will give you dd/mm/yyyy as stated in the question).


    SQL Fiddle (Shamelessly stolen from @bluefeet, who beat me to the answer). I’ve left this answer here though as it is slightly different as it will provide 13 columns (for the last year and current month) whether or not data exists for that month in the table.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The

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.