Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2009-01-16 : 07:39:38
|
| I have tables template.Test, and create a series of tables based on that template called:dbo.Test_20090101dbo.Test_20090102dbo.Test_20090103dbo.Test_20090104dbo.Test_20090105..............I only want to identity the tables that are older than 2 days old, for example. The following query gets all the tables based on the template:[code]SELECT tbls.object_id, tbls.[name]FROM sys.tables sysTbls INNER JOIN sys.schemas schm ON schm.schema_id = sysTbls.schema_idINNER JOIN sys.tables tbls ON REPLACE(tbls.[name],RIGHT(tbls.[name],9),'') = sysTbls.[name]WHERE schm.[name] = 'template'AND RIGHT(tbls.[name],8) < CONVERT(VARCHAR,@OldestPartitionDate)[/code]The part highlighted in RED is where I am having issues. I want to convert RIGHT(tbls.[name],8) to a DATETIME rather than CONVERT(VARCHAR,@OldestPartitionDate), because as it is, yields incorrect results (not an INT or datetime, so does not perform a < correctly):CONVERT(DATETIME,RIGHT(tbls.[name],8),112) < @OldestPartitionDateHowever, I just cannot make it work without getting the error:Msg 241, Level 16, State 1, Line 16Conversion failed when converting datetime from character string.Heeeeeeeeeeeeeeelp............Hearty head pats |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-16 : 07:42:20
|
| WHERE CAST(RIGHT(tbls.[name],8) as DATETIME) < @OldestPartitionDateMadhivananFailing to plan is Planning to fail |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2009-01-16 : 07:58:45
|
Still fails See example:BEGIN TRANCREATE SCHEMA templateGOCREATE TABLE template.Test (id int)CREATE TABLE dbo.Test_20090101 (id int) CREATE TABLE dbo.Test_20090102 (id int) CREATE TABLE dbo.Test_20090103 (id int) CREATE TABLE dbo.Test_20090104 (id int) CREATE TABLE dbo.Test_20090105 (id int) DECLARE @OldestPartitionDate DATETIMESET @OldestPartitionDate = CURRENT_TIMESTAMP-2SELECT tbls.object_id, tbls.[name],CONVERT(VARCHAR,RIGHT(tbls.[name],8),112)FROM sys.tables sysTbls INNER JOIN sys.schemas schm ON schm.schema_id = sysTbls.schema_idINNER JOIN sys.tables tbls ON REPLACE(tbls.[name],RIGHT(tbls.[name],9),'') = sysTbls.[name]WHERE schm.[name] = 'template'AND CAST(RIGHT(tbls.[name],8)AS DATETIME) < @OldestPartitionDateROLLBACK TRAN Hearty head pats |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-16 : 08:11:09
|
| TryWHERE schm.[name] = 'template'AND ISDATE(RIGHT(tbls.[name],8))=1 and LEN(RIGHT(tbls.[name],8))=8 and CAST(RIGHT(tbls.[name],8)AS DATETIME) < @OldestPartitionDateMadhivananFailing to plan is Planning to fail |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2009-01-16 : 08:19:01
|
| Hi MadhivananThat works!!!! Thank you!Could you explain to me WHY that works? I Understand that you're checking that the value is in fact a date, then checking the length, and then casting the value.However, why would we need to check the length if we have already confirmed that the value is in fact a date? What is going on within SQL Server????Hearty head pats |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-16 : 09:12:39
|
quote: Originally posted by Bex Hi MadhivananThat works!!!! Thank you!Could you explain to me WHY that works? I Understand that you're checking that the value is in fact a date, then checking the length, and then casting the value.However, why would we need to check the length if we have already confirmed that the value is in fact a date? What is going on within SQL Server????Hearty head pats
just checking alone ISDATE(value) =1 doesnt ensure that its a valid date value. try the belowSELECT ISDATE(2007)ISDATE tries to convert passed on value to valid date and if it can it will return 1 (2007 will be converted to 2007-01-01 00:00:00.000)so inorder to check that you've a full date value of format yyyy-mm-dd Madhi has added condition LEN(RIGHT(tbls.[name],8))=8 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-19 : 01:15:15
|
| http://sqlblogcasts.com/blogs/madhivanan/archive/2007/09/24/handle-isdate-with-care.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|