Rename table using dynamic SQL

Posted on 2005-02-25
Medium Priority
Last Modified: 2012-06-21
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 . . .

Question by:rikroket
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 13

Expert Comment

ID: 13407741
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
LVL 21

Accepted Solution

Kevin3NF earned 2000 total points
ID: 13407772

Declare @NewName varchar(100)
Set @NewName = cast(datepart(mm,getdate())as varchar(2))
                +cast(datepart(dd,getdate())as varchar(2))
                +cast(datepart(yyyy,getdate())as varchar(4))

Select @NewName = @NewName + '_Test'

exec sp_rename 'Test',@NewName
LVL 21

Expert Comment

ID: 13407783
Missed it by > that< much....well done KarinLoos
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

LVL 43

Expert Comment

by:Eugene Z
ID: 13408142
LVL 43

Expert Comment

by:Eugene Z
ID: 13408143
ops - wrong post
LVL 21

Expert Comment

ID: 13408314
Hey rikroket....as much as I appreciate the points....I was second with the answer. :-)

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question