SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Capturing TableName MonYY Name


Capturing TableName MonYY Name

Author
Message
DB_Newbie2007
DB_Newbie2007
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1070 Visits: 1346
I have some tables in a database which contain the month and year, i.e., CustAddresses_Dec07, CustAddresses_Nov07, etc. as part of the name.

In trimming the database, I wish to delete some of the older table(s) using a drop. However, the problem is trying to identify the tables to drop.

Here is the code I was trying to use to select the tables. Assume that I wish to drop all tables prior to 11/1/07:

SELECT name
,SUBSTRING(NAME,LEN(Name)-4,3)
,CONVERT(SmallDateTime,SUBSTRING(NAME,LEN(Name)-4,3)+ ' 01, '+SUBSTRING(NAME,LEN(Name)-1,2),101) as CastName2Date
,CONVERT(SmallDateTime,'2007-11-01',101) as CastTimeEntered
,DATEDIFF(mm,CONVERT(SmallDateTime,'2007-11-01',101),CONVERT(SmallDateTime,SUBSTRING(NAME,LEN(Name)-4,3)+ ' 01, '+SUBSTRING(NAME,LEN(Name)-1,2),101))
,DATEDIFF(mm,'2007-11-01',SUBSTRING(NAME,LEN(Name)-4,3)+ ' 01, '+SUBSTRING(NAME,LEN(Name)-1,2))
--,DATENAME(MM,(SUBSTRING(NAME,LEN(Name)-4,3))+ '01 20'+SUBSTRING(NAME,LEN(Name)-1,2))
from dbo.SysObjects
WHERE LEFT(Name,5) = 'CustA'
AND SUBSTRING(NAME,LEN(Name)-4,3) IN ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
AND DATEDIFF(mm,'2007-11-01',SUBSTRING(NAME,LEN(Name)-4,3)+ ' 01, '+SUBSTRING(NAME,LEN(Name)-1,2)) < 0

The problem is with the DateDiff in the WHERE clause. The SELECT works fine in concatenating the name to get a DateDiff value, but when I try to use the DateDiff in the WHERE clause I receive a Conversion failed when converting datetime from character string. error message.

Any ideas? Comments?

Thanks!

Argue for your limitations, and sure enough they're yours (Richard Bach, Illusions)
Gayathri.Varadarajan
Gayathri.Varadarajan
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1200 Visits: 388
Seems this script is working fine.Can you just send across your drop table script.

select ' drop table ' + name
from dbo.SysObjects
WHERE LEFT(Name,5) = 'CustA'
AND SUBSTRING(NAME,LEN(Name)-4,3) IN ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
AND DATEDIFF(mm,'2007-11-01',SUBSTRING(NAME,LEN(Name)-4,3)+ ' 01, '+SUBSTRING(NAME,LEN(Name)-1,2)) < 0
DB_Newbie2007
DB_Newbie2007
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1070 Visits: 1346
Hmmm.... no matter what I did, I could not get the WHERE clause to use the DATEDIFF function without getting a conversion error message.

I re-did the process... here it is:

DECLARE @Statement NVARCHAR(4000),
@MyTable NVARCHAR(128),
@TableDate smalldatetime,
@DeleteDate smalldatetime,
@DataCntr INT,
@LoopCntr INT

SET @DeleteDate = '2007/11/01'
IF OBJECT_ID(N'tempdb..#temptable', N'U') IS NOT NULL
DROP TABLE #temptable;

CREATE TABLE #temptable ( primary_key INT IDENTITY(1,1) NOT NULL,
[MyTable] nvarchar(128) null)

INSERT into #temptable
SELECT name
FROM dbo.SysObjects
WHERE LEFT(Name,5) = 'CustA'
AND SUBSTRING(NAME,LEN(Name)-4,3) IN
('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
-- Sent Counters for looping purposes
SET @DataCntr = ISNULL((Select count(*) from #temptable),0)
SET @LoopCntr = 1
WHILE @DataCntr > 0 AND @LoopCntr <= @DataCntr
BEGIN -- WHILE LOOP
---- CODE GOES HERE, this is an example
SELECT @MyTable = MyTable
FROM #temptable
WHERE primary_key = @LoopCntr
---- CODE GOES HERE
SET @TableDate = CONVERT(SmallDateTime,SUBSTRING(@MyTable,LEN(@MyTable)-4,3)+ ' 01, '+SUBSTRING(@MyTable,LEN(@MyTable)-1,2),101)
IF (DATEDIFF(MM,@DeleteDate,@TableDate) < 0)
BEGIN
PRINT 'Table '+@MyTable+' is older';
SET @Statement = 'DROP TABLE '+@MyTable;
PRINT @Statement;
-- EXEC (@Statement);
END
ELSE PRINT 'Do not touch '+@MyTable
SET @LoopCntr = @LoopCntr + 1;
END -- WHILE LOOP
DROP TABLE #temptable;

Setting the CONVERT to a variable, then using it in the DATEDIFF function works fine!

Argue for your limitations, and sure enough they're yours (Richard Bach, Illusions)
DB_Newbie2007
DB_Newbie2007
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1070 Visits: 1346
Please ignore the CODE GOES HERE comments... I use templates when I create some of these scripts. Wink

Argue for your limitations, and sure enough they're yours (Richard Bach, Illusions)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search