The ideal way, if you are allowed to do so, is to save the data into a SINGLE table. Since you have the time stamp, querying becomes very easy. Doing this way may make it appear that you have one huge table, and so querying would be difficult, but that is not the case - especially if you are allowed to index the time stamp column.If you do not have the freedom to change the design, you would likely need to write a dynamic query that unions two tables based on the date - something like this:DECLARE @sql NVARCHAR(4000), @tbl1 VARCHAR(64), @tbl2 VARCHAR(64);SET @tbl1 = 'e4_event_'+CONVERT(VARCHAR(8),GETDATE(),112);SET @tbl2 = 'e4_event_'+CONVERT(VARCHAR(8),DATEADD(dd,-1,GETDATE()),112);SET @sql = 'SELECT * FROM '+@tbl1+' WHERE YourTimeColumn > ''' + CONVERT(NVARCHAR(32),DATEADD(dd,-1,GETDATE()),126) + ''''+ 'UNION ALL ' +'SELECT * FROM '+@tbl2+' WHERE YourTimeColumn > ''' + CONVERT(NVARCHAR(32),DATEADD(dd,-1,GETDATE()),126) + ''''EXEC (@sql);
This is probably not the best way even for dynamic queries. You should parameterize it and use sp_executesql.