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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 CONVERT VARCHAR TO DATETIME

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_20090101
dbo.Test_20090102
dbo.Test_20090103
dbo.Test_20090104
dbo.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_id
INNER 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) < @OldestPartitionDate

However, I just cannot make it work without getting the error:

Msg 241, Level 16, State 1, Line 16
Conversion 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) < @OldestPartitionDate

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2009-01-16 : 07:58:45
Still fails

See example:
BEGIN TRAN
CREATE SCHEMA template
GO
CREATE 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 DATETIME

SET @OldestPartitionDate = CURRENT_TIMESTAMP-2

SELECT 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_id
INNER 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) < @OldestPartitionDate

ROLLBACK TRAN


Hearty head pats
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-16 : 08:11:09
Try

WHERE 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) < @OldestPartitionDate


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2009-01-16 : 08:19:01
Hi Madhivanan

That 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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-16 : 09:12:39
quote:
Originally posted by Bex

Hi Madhivanan

That 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 below

SELECT 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

Go to Top of Page

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.aspx


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -