Link to home
Start Free TrialLog in
Avatar of rikroket
rikroket

asked on

Rename table using dynamic SQL

I would like to rename a table by appending the current date to the front of a table name.
For example:

Rename this table: Table1
To: 02212005_Table1 (month, day, year)

How can I perform this using dynamic SQL or by using sp_rename?

Thanks in advance . . .

Rik
Avatar of KarinLoos
KarinLoos

declare @newname varchar(20)
set @newname =  convert(varchar(2), day(GetDate()) ) + convert( VARCHAR(2) , MONTH(GETdATE()))
               + convert(char(4), Year(getdate()))

set @newname  = @newname + '_abc'
exec sp_rename 'Table1', @newname
ASKER CERTIFIED SOLUTION
Avatar of Kevin Hill
Kevin Hill
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Missed it by > that< much....well done KarinLoos
ops - wrong post
Hey rikroket....as much as I appreciate the points....I was second with the answer. :-)