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)
 Deleting data from Table depending on Datefield

Author  Topic 

eljapo4
Posting Yak Master

100 Posts

Posted - 2010-12-08 : 09:44:11
Hi, I'm currently doing the following:
--1 Year Of Data
DELETE FROM TableA WHERE CurrDate < (GETDATE() - 365)
DELETE FROM TableB WHERE CurrDate < (GETDATE() - 365)


The DBA alerted me that the field CurrDate is defined as nchar(35) instead of Datetime.

Will the above delete code cause any issues?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-08 : 10:00:10
Really.
You need to see what the format of the date in the column is (and if it is consistent).

Yes the above code could cause issues.
It will convert the CurrDate value to a datetime using the connection settings. Will be likely to either be ok or fail but you might be unlucky.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

eljapo4
Posting Yak Master

100 Posts

Posted - 2010-12-08 : 10:17:16
The data in the CurrDate field is consistent and looks like this 'Feb 7 2010 12:00AM' what issues could I be faced with? I've been deleting data from a test DB with the above and it seems to have worked fine so far.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-08 : 11:04:26
That should be ok because the character column will unambiguously and implicitely convert to a datetime in your delete.
what you have is

DELETE FROM TableA WHERE convert(datetime,CurrDate) < GETDATE()-365

You have to be careful that no one comes along and notices the datatype mismatch and changes it to
DELETE FROM TableA WHERE CurrDate < convert(varchar(30),GETDATE()-365)

I would put in the explicit convert (of the CurrDate) to datetime to point out what you are doing.
Note this won't use an index seek as it has to convert the column so might be slow.


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-12-08 : 11:16:38
"That should be ok because the character column will unambiguously and implicitely convert to a datetime in your delete."

Provided the Language of the current user is English? or (I think) that the language implicitly divined from the Country of the connected user is English?

I agree, explicitly convert the CurrDate colume to DATETIME forcing the conversion to use the style of the field.

Better still get the column changed to Datetime datatype. Daft to be storing a Date/time value in CHAR datatype field
Go to Top of Page

eljapo4
Posting Yak Master

100 Posts

Posted - 2010-12-08 : 11:26:59
Nigel thank you for your advice on this
Go to Top of Page

eljapo4
Posting Yak Master

100 Posts

Posted - 2010-12-08 : 11:33:40
Well that's the next step of my investigation, i'm trying to track back to see why the value was set as nchar and not Datetime and hopefully from that change the field to be Datetime.
Go to Top of Page
   

- Advertisement -