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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:53:26+00:00 2026-06-02T13:53:26+00:00

Hi I am new to SQL server 2008 I want to expand a single

  • 0

Hi I am new to SQL server 2008

I want to expand a single row to multiple rows based on another colomn,

e.g

date          value
7-2011         5

Results:

2011-07-01     
2011-08-01
2011-09-01
2011-10-01
2012-11-01

the date shoild be first day of current and next month repeated 5 times

  • 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-02T13:53:27+00:00Added an answer on June 2, 2026 at 1:53 pm

    try:

    DECLARE @YourTable table (YourDate datetime, value int)
    
    insert into @YourTable VALUES ('2011-7-12',5)
    
    ;WITH AllNumbers AS
    (
        SELECT 0 AS Number
        UNION ALL
        SELECT Number+1
            FROM AllNumbers
            WHERE Number<4
    )
    SELECT 
        dateadd(month,number,DATEADD(month,DATEDIFF(month,0,YourDate),0))
        FROM @YourTable        y
        INNER JOIN AllNumbers  a ON 1=1
    

    output:

    -----------------------
    2011-07-01 00:00:00.000
    2011-08-01 00:00:00.000
    2011-09-01 00:00:00.000
    2011-10-01 00:00:00.000
    2011-11-01 00:00:00.000
    
    (5 row(s) affected)
    

    it works with multiple rows in the table, see here:

    DECLARE @YourTable table (YourDate datetime, ValueOf int)
    
    insert into @YourTable VALUES ('2011-7-12',5)
    insert into @YourTable VALUES ('2012-4-24',6)
    
    ;WITH AllNumbers AS
    (
        SELECT 0 AS Number
        UNION ALL
        SELECT Number+1
            FROM AllNumbers
            WHERE Number<4
    )
    SELECT 
        y.ValueOf
            ,dateadd(month,number,DATEADD(month,DATEDIFF(month,0,y.YourDate),0))
        FROM @YourTable        y
        INNER JOIN AllNumbers  a ON 1=1
        ORDER BY 1,2
    

    OUTPUT:

    ValueOf     
    ----------- -----------------------
    5           2011-07-01 00:00:00.000
    5           2011-08-01 00:00:00.000
    5           2011-09-01 00:00:00.000
    5           2011-10-01 00:00:00.000
    5           2011-11-01 00:00:00.000
    6           2012-04-01 00:00:00.000
    6           2012-05-01 00:00:00.000
    6           2012-06-01 00:00:00.000
    6           2012-07-01 00:00:00.000
    6           2012-08-01 00:00:00.000
    
    (10 row(s) affected)
    

    Also, I don’t have SQL Server 2008 available, so I used datetime, if you have 2008, you can use DATE datatype and you don’t have to floor the datetime, so use this line:

    dateadd(month,number,y.YourDate)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an entirely new Sql Server 2008 database and want to
Question: The new SQL Server 2008 database returns me values formatted English (date/float). Is
I love the new DATE datatype in SQL Server 2008, but when I compare
I want to Install New SQL Server 2008 Instance. I want to know whether
I'm creating a new table in a SQL Server 2008 database and I want
I am new to SQL Server 2008 fill factor, as mentioned here in SQL
I'm brand new to SQL Server 2008, and have some newbie questions about the
I am new to SQL Server 2008, and I need some help in query
using Visual.Web.Developer.2010.Express; using SQL.Server.Management.Studio.2008.R2; Kinda new at C# here.. I got the first row
I am using SQL Server 2008 Enterprise DB and using the new Merge statement

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.