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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:25:52+00:00 2026-05-28T04:25:52+00:00

I have tables named: Posts_August_2011 Posts_September_2011 Posts_October_2011 Posts_November_2011 Posts_December_2011 Posts_January_2012 I want to create

  • 0

I have tables named:

Posts_August_2011
Posts_September_2011
Posts_October_2011
Posts_November_2011
Posts_December_2011
Posts_January_2012

I want to create stored procedure that would UNION tables,but that is not the problem since I can use dynamic SQL to glue it up, my problem is getting Months and Year Range from RANGE to some kind of list? and then checking if table exist, Union them and return.

I would declare 2 variables @FromDate and @Todate as datetime

What would be the best way to extract month name and year for each of the months in given range.

Say I give it a range @FromDate = '2011-10-18 14:16:27.000' and
@ToDate = '2012-01-09 14:16:27.000'
can someone advise how to extract Month and year.

DECLARE @SqlToExecute as nvarchar(MAX);

using dinamyc SQL is acceptable,It could check for existence of table in while loop and
glue to string for each table that exists in range as:

@SqlToExecute += 'SELECT * FROM Posts_' + extractedMonthName + '_' + extractedYear + ' UNION ALL ';
  • 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-28T04:25:52+00:00Added an answer on May 28, 2026 at 4:25 am

    This problem is caused by bad database design. You needed to store rows from all this tables in one table Posts with date field to specify Month and Year.

    To get names of tables Posts_% and date extracted from their names you can use following code:

    create table Posts_August_2011(i int)
    GO
    create table Posts_September_2011(i int)
    GO
    
    DECLARE @SqlToExecute varchar(max)
    DECLARE @FromDate date = '20110801', @ToDate date = '20110809'
    
    SELECT  name PostName, CAST(REPLACE(REPLACE(name,'Posts_','01/'),'_','/') as date) PostDate
    INTO #Posts
    FROM sys.tables
    WHERE name like 'Posts_%'
    
    SET @SqlToExecute=''
    
    SELECT @SqlToExecute += 'SELECT * FROM ' + PostName + ' UNION ALL '
    FROM #Posts
    WHERE PostDate>=@FromDate
          AND PostDate<@ToDate
    
    SELECT SUBSTRING(@SqlToExecute,1, LEN(@SqlToExecute)-10)
    
    DROP TABLE #Posts
    DROP TABLE Posts_August_2011
    DROP TABLE Posts_September_2011
    

    sys. tables is system catalog view which returns a row for each table object in the current database.

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

Sidebar

Related Questions

I have ten or more(i don't know) tables that have a column named foo
I have a database named bbs, which have 37 tables. I want to find
I have two tables named foo and bar , hypothetically speaking. foo has columns
Let's say you have three tables named Item, Event, and Seat, constructed as such:
I have two tables, both named as say, Employee in two different schema HR
I have a table named visiting that looks like this: id | visitor_id |
I have two tables that I join on the id-column, they look like: +-------+
Quick question that requires a long explanation.. Say I have two tables - one
I have two tables. One that keeps all of my threads started on a
I have got two tables: threads: id, title posts: id, thread_id, body I want

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.