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
 SQLHelp With Datediff

Author  Topic 

otakubakaa
Starting Member

6 Posts

Posted - 2012-07-25 : 01:47:54
mods: please close thread

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-07-25 : 02:49:24
Can you please tell us the data type of Search_Date?
And if it is not DATE or DATETIME then please post an example on how the values are formatted in that column.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

otakubakaa
Starting Member

6 Posts

Posted - 2012-07-25 : 09:35:50
quote:
Originally posted by webfred

Can you please tell us the data type of Search_Date?
And if it is not DATE or DATETIME then please post an example on how the values are formatted in that column.


No, you're never too old to Yak'n'Roll if you're too young to die.



sorry, data type is datetime
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-07-25 : 09:38:31
DELETE FROM dbo.Table_Name WHERE Search_Date < DATEADD(MONTH, -2, GETDATE())



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 10:17:26
DELETE FROM dbo.Table_Name WHERE Search_Date < DATEADD(MONTH,DATEDIFF(MONTH, 0, GETDATE())-2,0)

if you want to consider start of day (0000 hrs) as boundary value

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 12:18:04
nope this will search from first date of the previous 2 month period


DELETE FROM dbo.Table_Name WHERE Search_Date < DATEADD(MONTH,DATEDIFF(MONTH, 0, GETDATE())-2,0)

see this simple example

DECLARE @dateval datetime

SET @dateval='20120815'

SELECT DATEADD(MONTH,DATEDIFF(MONTH, 0, @dateval)-2,0)





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-25 : 12:54:35
the datediff part returns number of months elapsed from basedate (1 jan 1900) till current datemonths. then it subtracts 2 from it to reach two months prior to current month. adding this month value again to basedate using dateadd causes it to return 1st of that month

see this for similar logics

http://visakhm.blogspot.com/2010/01/some-quick-tips-for-date-formating.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -