Reputation: 2403
I have a table dc201709 which is created dynmacially on every 1st of the month. So if its October it will be 201710 and so on.
DC is the prefix and 201709 is current year and month in yyyymm format
Table look like
ID Name Classs
1 ABC First
2 LMN second
I want to select only ID and Class from the above table.
which I can achieve from the below query
select ID,Class from dc201709
Now I want to make the select statement such that I dont want to hardcode the table name again on every 1st day of a month
something like :-
select ID,Class from dc+CONVERT(varchar(6),getdate(),112)
so that I dont want to change my select statement again and again.
Is there a way to achieve that?
When I try:
select * from dc+'CONVERT(varchar(6),getdate(),112)' It gives me an error
Incorrect syntax
Upvotes: 0
Views: 34
Reputation: 4475
You can use exec statement
DECLARE @str VARCHAR(200)
SET @str = 'select ID, Class from dc'+ CONVERT(varchar(6),getdate(),112)
exec(@str)
Upvotes: 1