I have hundreds of tables in SQL where they are named in the format such as dbo.exampledata_2024-01-01 and a new table is created every week. I want to pull the data I need from either the last 12 tables or the last 3 months based on the dates on the name? Is this possible?
I'm not entirely sure where to start with this problem
The following approach makes use of the
information_schema.tablesto locate the tables that match the given naming convention. Note, if the last 10 chars of a table name cannot be converted to a date the script will error. Please consider the following purely as an example of the complexities that will always arise from the multiple table approach you have now:Also note this dynamic query is juat a union of all the tables - you haven't specified any details of what that query should do.