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
 General SQL Server Forums
 New to SQL Server Programming
 Delete dates that is earlier or older

Author  Topic 

mibs
Starting Member

3 Posts

Posted - 2012-03-05 : 09:50:22
its quite confusing when it comes to dates (for me)

Delete From Table where date < '01/31/2001'

the above script is deleting dates that's before 01/31/2001, which is 01/30/2001 and so on right?


Delete From Table where date > '01/31/2001'

and this will be deleting dates that's after 01/31/2001, which is 02/01/2011 right?

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-03-05 : 09:56:14
use ISO standard dates and there can be no confusion.

'YYYYMMDD' (example '20120201' -- 1st February 2012)

otherwise there is the potential for your locale to screw things up.

so your example would become

DELETE FROM <table> WHERE <dateColumn> > '20010131'

Which would delete any row where <dateColumn> is greater than 31st January 2001.

If this is a DATETIME column this will delete any row where <DateColumn> is greater than 31 January 2001 AT MIDNIGHT. So it would delete anything ON the 31st of JAn as well.

the ISO standard for DATE TIMES is (the ms are optional)

'YYYY-MM-DDTHH:MM:SS.msms' -- example '2012-01-13T12:05:00.000' is 13th of January 2012 at 12:05 PM


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

mibs
Starting Member

3 Posts

Posted - 2012-03-05 : 22:10:46
ok for the format im using is MM/dd/yyyy

how it determine the dates are greater than when its deleting (datecolumn) > 01/31/2001?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-05 : 22:28:46
quote:
Originally posted by mibs

its quite confusing when it comes to dates (for me)

Delete From Table where date < '01/31/2001'

the above script is deleting dates that's before 01/31/2001, which is 01/30/2001 and so on right?


Delete From Table where date > '01/31/2001'

and this will be deleting dates that's after 01/31/2001, which is 02/01/2011 right?



change the DELETE to SELECT and you will see what are the rows return. These will be the rows that will be deleted


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mibs
Starting Member

3 Posts

Posted - 2012-03-06 : 00:47:49
thx khtan :D
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-03-06 : 04:13:52
quote:
Originally posted by mibs

ok for the format im using is MM/dd/yyyy

how it determine the dates are greater than when its deleting (datecolumn) > 01/31/2001?



Well then change it! Use ISO date formats.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-06 : 10:01:14
What's the datatype of the column?

datetime, datetime2 or date?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -